We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

"Notice: Access to undefined property id"

Hi phalconflyers,

I really need your help because I can't understand the behaviour of Volt engine here. However it's very simple. Context : I have 2 models, User and UserMessages. A User hasMany UserMessages. I defined my variable UserMessages to the view in my ControllerBase like this:

<?php
$this->view->userMessages = UserMessages::find(
                [
                    "user_id" => $this->session->get('user')->id,
                    "order" => "sentAt DESC",
                    "limit" => 4
                ]
 );

I can access to my variable in my view :

 {% for message in userMessages %}
   {{ dump(message) }}
 {% endfor %}

Result :

object(UserMessages)[79]
  public 'id' => string '1' (length=1)
  public 'user_id' => string '1' (length=1)
  public 'sender_id' => string '2' (length=1)
  public 'object' => string 'Mon 1er message' (length=15)
  public 'content' => string 'Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.Lorem ipsum.' (length=168)
  public 'opened' => string '0' (length=1)
  public 'sentAt' => string '0000-00-00 00:00:00' (length=19)
  protected '_dependencyInjector' => 
    object(Phalcon\Di\FactoryDefault)[10]
      protected '_services' => 
        array (size=26)
          'router' => 
            object(Phalcon\Di\Service)[11]
              ...
          'dispatcher' => 
            object(Phalcon\Di\Service)[12]
              ...
          'url' => 
            object(Phalcon\Di\Service)[33]
              ...
          'modelsManager' => 
            object(Phalcon\Di\Service)[14]
              ...
          'modelsMetadata' => 
            object(Phalcon\Di\Service)[38]
              ...
          'response' => 
            object(Phalcon\Di\Service)[16]
              ...
          'cookies' => 
            object(Phalcon\Di\Service)[47]
              ...
          'request' => 
            object(Phalcon\Di\Service)[18]
              ...
          'filter' => 
            object(Phalcon\Di\Service)[19]
              ...
          'escaper' => 
            object(Phalcon\Di\Service)[20]
              ...
          'security' => 
            object(Phalcon\Di\Service)[21]
              ...
          'crypt' => 
            object(Phalcon\Di\Service)[22]
              ...
          'annotations' => 
            object(Phalcon\Di\Service)[23]
              ...
          'flash' => 
            object(Phalcon\Di\Service)[39]
              ...
          'flashSession' => 
            object(Phalcon\Di\Service)[25]
              ...
          'tag' => 
            object(Phalcon\Di\Service)[26]
              ...
          'session' => 
            object(Phalcon\Di\Service)[46]
              ...
          'sessionBag' => 
            object(Phalcon\Di\Service)[28]
              ...
          'eventsManager' => 
            object(Phalcon\Di\Service)[29]
              ...
          'transactionManager' => 
            object(Phalcon\Di\Service)[30]
              ...
          'assets' => 
            object(Phalcon\Di\Service)[31]
              ...
          'view' => 
            object(Phalcon\Di\Service)[34]
              ...
          'db' => 
            object(Phalcon\Di\Service)[36]
              ...
          'message' => 
            object(Phalcon\Di\Service)[40]
              ...
          'config' => 
            object(Phalcon\Di\Service)[42]
              ...
          'logger' => 
            object(Phalcon\Di\Service)[44]
              ...
      protected '_sharedInstances' => 
        array (size=14)
          'router' => 
            object(Phalcon\Mvc\Router)[48]
              ...
          'view' => 
            object(Phalcon\Mvc\View)[52]
              ...
          'dispatcher' => 
            object(Phalcon\Mvc\Dispatcher)[51]
              ...
          'IndexController' => 
            object(IndexController)[54]
              ...
          'cookies' => 
            object(Phalcon\Http\Response\Cookies)[56]
              ...
          'modelsManager' => 
            object(Phalcon\Mvc\Model\Manager)[59]
              ...
          'session' => 
            object(Phalcon\Session\Adapter\Files)[57]
              ...
          'modelsMetadata' => 
            object(Phalcon\Mvc\Model\MetaData\Memory)[67]
              ...
          'db' => 
            object(Phalcon\Db\Adapter\Pdo\Mysql)[70]
              ...
          'Phalcon\Mvc\View\Engine\Php' => 
            object(Phalcon\Mvc\View\Engine\Php)[55]
              ...
          'config' => 
            object(Phalcon\Config)[3]
              ...
          'url' => 
            object(Phalcon\Mvc\Url)[73]
              ...
          'tag' => 
            object(Phalcon\Tag)[74]
              ...
          'escaper' => 
            object(Phalcon\Escaper)[69]
              ...
      protected '_freshInstance' => boolean false
      protected '_eventsManager' => null
  protected '_modelsManager' => 
    object(Phalcon\Mvc\Model\Manager)[59]
      protected '_dependencyInjector' => 
        object(Phalcon\Di\FactoryDefault)[10]
          protected '_services' => 
            array (size=26)
              ...
          protected '_sharedInstances' => 
            array (size=14)
              ...
          protected '_freshInstance' => boolean false
          protected '_eventsManager' => null
      protected '_eventsManager' => null
      protected '_customEventsManager' => null
      protected '_readConnectionServices' => null
      protected '_writeConnectionServices' => null
      protected '_aliases' => 
        array (size=3)
          'user$userprofil' => 
            object(Phalcon\Mvc\Model\Relation)[60]
              ...
          'user$usermessages' => 
            object(Phalcon\Mvc\Model\Relation)[61]
              ...
          'usermessages$messages' => 
            object(Phalcon\Mvc\Model\Relation)[64]
              ...
      protected '_modelVisibility' => 
        array (size=0)
          empty
      protected '_hasMany' => 
        array (size=1)
          'user$usermessages' => 
            array (size=1)
              ...
      protected '_hasManySingle' => 
        array (size=1)
          'user' => 
            array (size=1)
              ...
      protected '_hasOne' => 
        array (size=1)
          'user$userprofil' => 
            array (size=1)
              ...
      protected '_hasOneSingle' => 
        array (size=1)
          'user' => 
            array (size=1)
              ...
      protected '_belongsTo' => 
        array (size=1)
          'usermessages$messages' => 
            array (size=1)
              ...
      protected '_belongsToSingle' => 
        array (size=1)
          'usermessages' => 
            array (size=1)
              ...
      protected '_hasManyToMany' => null
      protected '_hasManyToManySingle' => null
      protected '_initialized' => 
        array (size=2)
          'user' => 
            object(User)[58]
              ...
          'usermessages' => 
            object(UserMessages)[63]
              ...
      protected '_prefix' => string '' (length=0)
      protected '_sources' => null
      protected '_schemas' => null
      protected '_behaviors' => null
      protected '_lastInitialized' => 
        object(UserMessages)[63]
          public 'id' => null
          public 'user_id' => null
          public 'sender_id' => null
          public 'object' => null
          public 'content' => null
          public 'opened' => null
          public 'sentAt' => null
          protected '_dependencyInjector' => 
            object(Phalcon\Di\FactoryDefault)[10]
              ...
          protected '_modelsManager' => 
            &object(Phalcon\Mvc\Model\Manager)[59]
          protected '_modelsMetaData' => null
          protected '_errorMessages' => null
          protected '_operationMade' => int 0
          protected '_dirtyState' => int 1
          protected '_transaction' => null
          protected '_uniqueKey' => null
          protected '_uniqueParams' => null
          protected '_uniqueTypes' => null
          protected '_skipped' => null
          protected '_related' => null
          protected '_snapshot' => null
          protected '_oldSnapshot' => 
            array (size=0)
              ...
      protected '_lastQuery' => null
      protected '_reusable' => null
      protected '_keepSnapshots' => null
      protected '_dynamicUpdate' => null
      protected '_namespaceAliases' => null
  protected '_modelsMetaData' => null
  protected '_errorMessages' => null
  protected '_operationMade' => int 0
  protected '_dirtyState' => int 0
  protected '_transaction' => null
  protected '_uniqueKey' => null
  protected '_uniqueParams' => null
  protected '_uniqueTypes' => null
  protected '_skipped' => null
  protected '_related' => null
  protected '_snapshot' => null
  protected '_oldSnapshot' => 
    array (size=0)
      empty

But when i'm trying to access directly to the existing property:

{% for message in userMessages %}
    {{ dump(message.id) }}
{% endfor %}

I have the following error :

"Notice: Access to undefined property id"

I checked all the forums, the syntax is good, my property exist and is public( I can print it in my controller ). So... What happend ? Have you got an idea ?

Thank you very much



32.3k
Accepted
answer

hi @maxfouquet it's very strange. Can you check the php file generated by Volt? probably there we have a solution or a clue

Good luck

You're right ! I found the solution in the php file ...

<?php foreach ($userMessages as $message) { ?>
       <?= var_dump($this->message->id) ?>
<?php } ?>

View called $this->message because I have a global class "Message" di injected. Issue was variable name conflict.

Thank you very much