【问题标题】:Yii: Trying to get property of non-object in viewYii:试图在视图中获取非对象的属性
【发布时间】:2017-05-24 12:38:28
【问题描述】:

我试图在视图中访问模型的属性,它向我抛出了标题中提到的错误。这是我的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',
                        ),

                    ),
                ));

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

【问题讨论】:

  • 我认为$data 是一个数组
  • 天哪,你救了我的命!非常感谢!

标签: php gridview yii model


【解决方案1】:

主要有两点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多