Yii Framework未定义变量模型

时间:2015-02-20 06:54:40

标签: php yii model undefined

我尽力解决这个问题,但我似乎无法找出问题所在。这是我的代码:

视图/超市/ 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中的模型(&#39; dataProvider&#39; =&gt; $ model-&gt; search()) 我正在尝试根据以下问题How to add Search and Filter Criteria in Yii

添加搜索和过滤条件

你能帮我吗?

3 个答案:

答案 0 :(得分:1)

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));

这将是无用的,因为你在此之前返回,

因此您可以抓取该变量并将其与第一个渲染一起渲染

答案 1 :(得分:0)

这会奏效。您已渲染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,
        ));

    }

答案 2 :(得分:0)

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

$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));