PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

[FIXED] Yii: Trying to get property of non-object in view

 March 19, 2022     gridview, model, php, yii     No comments   

Issue

I am trying to access attributes of the model in the view and it is throwing me the error mentioned in the title. This is my CompanyMask model:

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

Now In the view I am trying to access the name of the company like this:

$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',
                        ),

                    ),
                ));

What am I doing wrong here? Any help?


Solution

It's about two things:

  1. You are using $data as object, but it's an array: $data['company']->name
  2. You are using single quotes, so the value is the literal value $data->company->name instead of the real value. Remove the single quotes around $data['company']->name


Answered By - schellingerht
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing