I just stumbled upon this freaky behavior that is about trying to get a related model with twig, I believe is more about Twig itself rather than an Yii2 issue… that’s my problem:
I have a model that extends ActiveRecord and I have declared a one-to-many relationship through getter method in this way:
public function getChecklistHccp() {
return $this->hasOne(ChecklistHccp::className(), ['id' => 'id_checklist_hccp']);
}
Then in a twig view I am trying to get the related model in the usual way:
{% set checklist = model.checkListHccp %}
However the checklist object belongs to ActiveQuery class instead of ChecklistHccp (that extends ActiveRecord) the only way (so far) to make this works is calling the getter method directly:
{% set checklist = model.getCheckListHccp().one() %}
This indeed will skip the supercool Yii2 support for objects properties definition based on getter and setter class methods, and I am just wondering why… (on normal php view everything works as expected). Thanks everybody.