Hi there everybody!
Today I stumbled upon a strange exception that (I suppose) is related to Twig template engine:
I have an ActiveRecord model (Invoice.php) that have a one to many relation with User ActiveRecord model and is simply declared like this:
class Invoice extends yii\db\ActiveRecord {
...
/**
* @return \yii\db\ActiveQuery
*/
public function getCustomer() {
return $this->hasOne(User::className(), ['id' => 'id_customer']);
}
...
}
Whilst in the User class I have a method to get the user information based on its role:
class User extends yii\db\ActiveRecord implements IdentityInterface {
...
public function getDisplayName() {
if ($this->isCustomer()) {
$customerProfile = $this->customerProfile;
return $customerProfile->name . ' ' . $customerProfile->surname;
}
if ($this->isAgent()) {
$agentProfile = $this->agentProfile;
return $agentProfile->name . ' ' . $agentProfile->surname;
}
return $this->name;
}
...
}
The strange thing happens when I try to access to the customer related attribute of the Invoice model from a Twig view file:
{{ model.customer.displayName }}
or:
{{ model.customer.getDisplayName() }}
Having the following Exception:
[i]Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("Undefined offset: 139990982734824").
Caused by: yii\base\ErrorException
Undefined offset: 139990982734824
in /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/db/BaseActiveRecord.php at line 279[/i]
Whereas if I invoke the getter method directly everything works fine:
{{ model.getCustomer().one().getDisplayName() }}
And everything works fine if I access from a normal php view file
<?= $model->customer->displayName ?>
Surely is something I forgot thta is related to the Twig template but I cannot figure out what is exaclty, hope somebody as already experienced this situation and I will appreciate any help, thank you!!