Hello everyone, I’ve been using Yii2 everyday for the last 4 years and I can’t get over this mysterious issue.
I have a for loop in which I access a related model if the id in the main model is set. Like this:
<?php
/**
* @property $modelB_id
*/
class ModelA extends yii\db\ActiveRecord
{
public function getModelB()
{
return $this->hasOne(ModelB::class, ['id' => 'modelB_id']);
}
}
class ModelB extends yii\db\ActiveRecord
{}
$models = ModelA::find()->where(/** blabla */);
foreach ($models->each() as $modelA) {
echo $modelA->modelB_id; //outputs a number
var_dump($modelA->modelB); //outputs NULL
if ($modelA->modelB_id) {
$modelB = $modelA->modelB;
//expect $modelB to not be null
}
}
Now this works great for many models until I get one that has a valid $modelB_id set (there’s even a FK constraint involved) but upon calling $modelA->modelB it returns null, so my code fails later on with
Exception: Trying to get property of non-object when I try to access properties of modelB.
Do you have any idea of what may cause this and what steps I could take to further investigate the issue? I’m kinda stuck.
Thanks