I have an issue where I have an external attribute I am trying to access within a method generated via a database table relation. I am not sure why when I set the value of the attribute before the method is called via eager loading the value of the attribute is seen as empty.
Code snippets are as follows:
class Module extends \yii\db\ActiveRecord
{
protected $externalAttribute;
public function setExternalAttribute($value){
$this->externalAttribute = 1;
}
public function getExternalAttribute(){
return $this->externalAttribute;
}
public function getUserModules($parameters){
$join = [];
if( !empty($this->getExternalAttribute()) ){
$join = ['children'=>function($query){
$query->andWhere('children.Module_Id <> children.Parent ')->orderBy('children.Order, children.Module_Name');
},
'children.childrenStatus'=>function($query){
$query->andWhere('childrenStatus.Status = :status',[':status' => "Installed"]);
}
];
$module = Module::find()->where('module.Status = (SELECT Status_Id FROM status WHERE Status = "Installed") ')->innerJoinWith($join)->orderBy('Order, Module_Name')->all();
}
return $module;
}
public function getChildren()
{
if( !empty($this->getExternalAttribute) ){
return $this->hasMany(Module::className(), ['Parent' => 'Module_Id'])->where('menuItems.Status = (SELECT Status_Id FROM status WHERE Status = "Installed") ')
->from(['children' => Module::tableName()])->innerJoinWith(['menuItems'=>function($query){
}
]);
}
}
}
$module = new Module();
$module->setExternalAttribute(1);
$modules = $module->getUserModules($parameters);
Based on my class highlighted above, when the getUserModules method is called, checking the getExternalAttribute, the externalAttribute attribute has a value of one, but when the active record find method is executed which executes the getChildren method, the externalAttribute is now empty. Could someone provide some clarity on what I would have to do to maintain the value when the eager loaded relation is triggered.