Yii框架未定义变量模型


Yii Framework Undefined Variable model

我尽力解决了这个问题,但似乎不知道问题出在哪里

views/supermarkets/index.php:

<?php
use yii'helpers'Html;
use yii'widgets'LinkPager;
?>
<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.pp:

<?php
namespace app'models;
use yii'db'ActiveRecord;
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 app'controllers;
use yii'web'Controller;
use yii'data'Pagination;
use app'models'Supermarkets;
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())我正试图根据以下问题添加搜索和筛选条件如何在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));