Yii Framework 未定义变量模型

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.php:

<?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() ) 我正在尝试根据以下问题添加搜索和过滤条件

你能帮帮我吗?

在你的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));