Yet another Twig trouble with model getter

Good morning everybody!

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.

maybe it could be related to the sandbox, is ChecklistHccp into the twig sandbox?

Thanks for the answer, but nope, I did not use sandbox tag on my view, here I paste it:




{{ use('/app/widgets/AttachmentsInput') }}

{% set inputParams = model.getIsCompleted() ? {'class' : 'form-control action-date', 'readonly' : 'true'} : {'class' : 'form-control action-date'} %}


<div class="row">

  <div class="col-xs-12 col-sm-12">

    {{ form.field(model, 'done_at').widget('\\yii\\widgets\\MaskedInput', {

        'mask': '99/99/9999',

        'options' : inputParams

    }) | raw }}

  </div>

</div>

 

<div class="row">

  <div class="col-xs-12 col-sm-12">{{ attachments_input_widget({'model' : model }) | raw }}

  </div>

</div>

  

  <div class="row">

    <div class="col-xs-12 col-sm-12">

      {% set check =  model.getChecklistHccp().one() %}

      {% set buttonText = check is null ? 'Compila Checklist HCCP' : 'Modifica Checklist HCCP' %}

      {{ html.a('<i class="fa fa-pencil" aria-hidden="true"></i> ' ~ buttonText, url.to({0: '/checklist-hccp/checklist', 'id': model.id }), {'class' : 'btn btn-default'}) | raw }}

    </div>

  </div>