Trying to get property of non-object

Any help with this problem would be most appreciated. Thanks

Table_Campaign has a foreign key to Table_Company. In views/campaign/view.php file I want to display the company name from Table_Company. I am using the following code:

<?= DetailView::widget([


    'model' => $model,


    'attributes' => [


        //'company',


        [


            'label' => 'Company',


            'value' => $model->company->name,


        ],


        'name',


        'comment:ntext',


        'active',


        'cron',


        'last_updated',


        'created',


    ],


]) ?>

but it is generating a "trying to get property of non-object" error.

My models/campaigns.php contains this snippet:

/**


 * @return \yii\db\ActiveQuery


 */


public function getCompany()


{


    return $this->hasOne(Company::className(), ['id' => 'company_id']);


}

but I notice that if I dump the $model, it does not contain the data from the company table:

app\models\Campaign Object

(

[campaignActive] => Array


    (


        [0] => CLOSED


        [1] => ACTIVE


    )








[_attributes:yii\db\BaseActiveRecord:private] => Array


    (


        [id] => 3


        [name] => btest


        [comment] => 


        [company] => 2


        [active] => 1


        [cron] => 0


        [last_updated] => 2016-08-06 00:07:10


        [created] => 2016-08-06 00:07:10


    )





[_oldAttributes:yii\db\BaseActiveRecord:private] => Array


    (


        [id] => 3


        [name] => btest


        [comment] => 


        [company] => 2


        [active] => 1


        [cron] => 0


        [last_updated] => 2016-08-06 00:07:10


        [created] => 2016-08-06 00:07:10


    )





[_related:yii\db\BaseActiveRecord:private] => Array


    (


    )





[_errors:yii\base\Model:private] => 


[_validators:yii\base\Model:private] => 


[_scenario:yii\base\Model:private] => default


[_events:yii\base\Component:private] => Array


    (


    )





[_behaviors:yii\base\Component:private] => Array


    (


    )

)

I have spent several hours trying to correct this with no luck. I even re-installed the files using gii but no luck.

It seems to me that the two tables are not linked but I am sure they are. Here is the SHOW CREATE TABLE code:

CREATE TABLE campaign (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(50) NOT NULL,

comment mediumtext,

company int(11) NOT NULL,

active tinyint(1) NOT NULL DEFAULT ‘0’,

cron tinyint(1) NOT NULL DEFAULT ‘0’,

last_updated datetime NOT NULL,

created datetime NOT NULL,

PRIMARY KEY (id),

UNIQUE KEY ukey_name (name),

CONSTRAINT fk_company FOREIGN KEY (company) REFERENCES company (id)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

CREATE TABLE company (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(50) NOT NULL,

comment mediumtext,

active tinyint(1) NOT NULL DEFAULT ‘0’,

last_updated datetime NOT NULL,

created datetime NOT NULL,

PRIMARY KEY (id),

UNIQUE KEY ukey_name (name)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

Any help would be most appreciated. I don’t know what is going on…

Your relation snippet on model references to company_id but on your schema you have company field.

So change it to




public function getCompany()

{

return $this->hasOne(Company::className(), ['id' => 'company']);

}



Thanks for that BadYusuke!

That was an oversight on my part. Unfortunately it wasn’t the cause of the problem. For anybody else who may be experiencing this problem, it turned out that both the parent and child table had the same field names in both (ie collector.name and campaign.name)

This was causing ambiguity in the sql search. The solution was to fully qualify the table and field as follows:

    $query->andFilterWhere(['like', 'collector.name', $this->name]) // fully qualified

rather than:

    $query->andFilterWhere(['like', 'name', $this->name]) // sql doesn't know which table I am referring to

I hope this helps somebody else.

Thanks