Yii:试图在视图中获取非对象的属性

时间:2017-05-24 12:38:28

标签: php gridview yii model

我试图在视图中访问模型的属性,它正在向我提出标题中提到的错误。这是我的CompanyMask模型:

public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'company' => array(self::BELONGS_TO, 'company', 'company_id'),
        );
    }

 public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

//        $criteria->select = "t.id, t.company_id, t.mask_id, t.mask_text, c.name as company_name";
//        $criteria->join = 'LEFT JOIN company c on c.id=t.company_id';


        $criteria->with = array('company');

        $criteria->compare('id', $this->id);
        $criteria->compare('company_id', $this->company_id);
        $criteria->compare('mask_id', $this->mask_id);
        $criteria->compare('mask_text', $this->mask_text, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

现在在视图中我试图访问公司名称,如下所示:

$gridView = $this->widget('zii.widgets.grid.CGridView', array(
                    'id' => 'deals-grid',
                    'dataProvider' => $model->search(),
                    'filter' => $model,
                    'ajaxUpdate' => 'deals-grid',
                    'itemsCssClass' => 'table table-striped table-responsive table-bordered no-margin',
                    'pagerCssClass' => 'pagination pagination-sm no-margin pull-right',
                    'pager' => array(
                        'maxButtonCount' => '7',
                    ),
                    'columns' => array(
                        array(
                            'header' => 'Company Name',
                            'type' => 'raw',
                            'htmlOptions' => array('style' => 'width:15%',),
                            'value' => '$data->company->name',
                        ),

                    ),
                ));

我在这里做错了什么?有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

它涉及两件事:

  1. 您使用$data作为对象,但它是一个数组:$data['company']->name
  2. 您使用的是单引号,因此value是字面值$data->company->name而不是实际值。删除$data['company']->name
  3. 周围的单引号