Yii Framework未定义变量模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii Framework未定义变量模型相关的知识,希望对你有一定的参考价值。

我尽力解决这个问题,但我似乎无法弄清楚问题出在哪里。这是我的代码:

意见/超市/ index.php文件:

<?php
use yiihelpershtml;
use yiiwidgetsLinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
    $array = (array) $supermarkets;


    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'supermarkets-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'name',
            'location',
            'telephone',
            'fax',
            'website'
        ),
    ));



function build_table($array){

    // start table

    $html = '<table class="altrowstable" id="alternatecolor">';

    // header row

    $html .= '<tr>';

    foreach($array[0] as $key=>$value){

            $html .= '<th>' . $key . '</th>';

        }

    $html .= '</tr>';

    // data rows

    foreach( $array as $key=>$value){

        $html .= '<tr>';

        foreach($value as $key2=>$value2){

            $html .= '<td>' . $value2 . '</td>';

        }

        $html .= '</tr>';

    }

    // finish table and return it

    $html .= '</table>';

    return $html;

}



echo build_table($array);

?>


<?= LinkPager::widget(['pagination' => $pagination]) ?>

Supermarkets.php:

<?php

namespace appmodels;

use yiidbActiveRecord;

class Supermarkets extends ActiveRecord
{

 public function search()
{

    $criteria=new CDbCriteria;

    $criteria->compare('name',$this->Name,true);
    $criteria->compare('location',$this->Location,true);
    $criteria->compare('telephone',$this->Telephone,true);
    $criteria->compare('fax',$this->Fax,true);
    $criteria->compare('website',$this->Website,true);

    return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'name ASC',
        ),
        'pagination'=>array(
            'pageSize'=>20
        ),
    ));
}

SupermarketsController.php:

<?php

namespace appcontrollers;

use yiiwebController;
use yiidataPagination;
use appmodelsSupermarkets;

class SupermarketsController extends Controller
{
    public function actionIndex()
    {
        $query = supermarkets::find();

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $supermarkets = $query->orderBy('Name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return $this->render('index', [
            'supermarkets' => $supermarkets,
            'pagination' => $pagination,
        ]);



        $model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];

        return  $this->render('index', array('model'=>$model));

    }
    public function actionName(){

    $model = new Supermarkets();



    $this->render('index', array('model'=>$model));
}
}

这是我得到的错误:

未定义的变量:index.php中的模型('dataProvider'=> $ model-> search())我试图根据以下问题添加搜索和过滤条件How to add Search and Filter Criteria in Yii

请你帮助我好吗?

答案

在你的SupermarketsController.php中:你有一个错误:

你已经渲染了这个

    return $this->render('index', [
        'supermarkets' => $supermarkets,
        'pagination' => $pagination,
    ]);

它没有任何模型命名变量,

后来你有

    $model =new Supermarkets('search');
    if(isset($_GET['Supermarkets']))
        $model->attributes =$_GET['Supermarkets'];

    return  $this->render('index', array('model'=>$model));

这将是无用的,因为你在那之前回来,

所以你可以抓住那个变量并将它与第一个渲染一起渲染

另一答案

这会奏效。您已渲染2时间索引视图。将它们合并为一个

 public function actionIndex()
    {
        $query = supermarkets::find();

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $supermarkets = $query->orderBy('Name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();



        $model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];


        return  $this->render('index', array(
            'model'        => $model,
            'supermarkets' => $supermarkets,
            'pagination'   => $pagination,
        ));

    }
另一答案

你在控制器中渲染了两次,

$model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];


        return  $this->render('index', array(
            'model'        => $model,
            'supermarkets' => $supermarkets,
            'pagination'   => $pagination,
        ));

请在上面的代码中替换以下代码

return $this->render('index', [
            'supermarkets' => $supermarkets,
            'pagination' => $pagination,
        ]);



        $model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];

        return  $this->render('index', array('model'=>$model));

以上是关于Yii Framework未定义变量模型的主要内容,如果未能解决你的问题,请参考以下文章

为什么变量没有未定义?

yii2.0 framework怎么用

Yii Framework使用DAO方式查询分页代码

YII数据流程浅析

Yii2 ListView 自定义数据属性基于当前 DP 模型

Yii新建项目cmd