Troubles while access related attributes with twig

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!!

So far I have tried to empty the Twig cache directory without success but I suspect is something related to the way Twig generate and cache the PHP code to build a view page, here I paste the function calls stack trace:

[i]exception ‘yii\base\ErrorException’ with message ‘Undefined offset: 140677889419600’ in /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/db/BaseActiveRecord.php:279 Stack trace:

#0 /var/www/apache/plurima.snake.lappi.virt/htdocs/runtime/Twig/cache/a4/a419d0ece03caf981e8cf7fb7324d0e998a9d118245cf51010a04997e0a1ab0c.php(43): yii\base\Model->offsetGet()

#1 /var/www/apache/plurima.snake.lappi.virt/htdocs/runtime/Twig/cache/a4/a419d0ece03caf981e8cf7fb7324d0e998a9d118245cf51010a04997e0a1ab0c.php(43): ::twig_template_get_attributes()

#2 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_2783c743d8c4bac31b0f98ef78b451da39a675cf4e71101414853d67d352d886->doDisplay()

#3 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling()

#4 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/twig/twig/lib/Twig/Template.php(411): Twig_Template->display()

#5 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/twig/twig/lib/Twig/Environment.php(362): Twig_Template->render()

#6 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2-twig/ViewRenderer.php(175): Twig_Environment->render()

#7 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/View.php(248): yii\twig\ViewRenderer->render()

#8 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile()

#9 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/Controller.php(381): yii\base\View->render()

#10 /var/www/apache/plurima.snake.lappi.virt/htdocs/controllers/OrderController.php(82): yii\base\Controller->render()

#11 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/InlineAction.php(57): app\controllers\OrderController->actionView()

#12 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/InlineAction.php(57): ::call_user_func_array:{/var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/InlineAction.php:57}()

#13 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/Controller.php(156): yii\base\InlineAction->runWithParams()

#14 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction()

#15 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction()

#16 /var/www/apache/plurima.snake.lappi.virt/htdocs/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest()

#17 /var/www/apache/plurima.snake.lappi.virt/htdocs/web/index.php(15): yii\base\Application->run()

#18 {main}[/i]

At the end of the day it was my fault, I declared a Behavior that was looking for an attribute that did not exists in the behavior owner class. However debugging with Twig can be tricky sometimes.