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

Can't output Model result in view

I have strang issue, fetched data using model, passed to view but view is always giving error :

 Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$id in D:\phalcon\cache\volt\d__server_www_phalcon3_ph_app_views_track_track.volt.php on line 17

I have following code: In controller :

    if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost()) == false) {
                foreach ($form->getMessages() as $message) {
                    $this->flash->error($message);
                }
                return $this->dispatcher->forward([
                            "action" => "index"
                ]);
            } else {
                $issue = Issues::find(
                                [
                                    'conditions' => 'idDisplay = :idDisplay:',
                                    'bind' => [
                                        'idDisplay' => $this->request->getPost('idDisplay', 'striptags'),
                                    ]
                                ]
                );
                 if (count($issue)==0 ) {
                    $this->flash->error("No Issue records found with given ID");
                    return $this->dispatcher->forward([
                                'action' => 'index'
                    ]);
                }
                $this->view->issues = $issues;
            }

In volt / view

 <div class="card card-body">
      {{ issue.id  }}
</div>

Vardump for $issues->toArrary()

    array (size=1)
  0 => 
 array (size=7)
  'id' => string '30' (length=2)
  'idDisplay' => string 'SOU08-180625-4434' (length=17)
  'idUnitType' => string '5' (length=1)
  'idUnits' => string '7' (length=1)
  'idUsers' => string '11' (length=2)
  'date' => string '1529916274' (length=10)
  'description' => string '' (length=0)

And Error I am seeing everytime

  Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$id in D:\phalcon\cache\volt\d__server_www_phalcon3_ph_app_views_track_track.volt.php on line 17

It is same error for other properties too.



77.7k
Accepted
answer
edited Jun '18

You are trying to access Model properties on a ResultSet

Model::find() will return MULTIPLE records (a ResultSet).

Model::findFirst() will return a SINGLE record (a Model).

As you've shown, the var_dump displays a nested array. First level for the rows, second for the columns.

Either loop through the results in the volt template. or just use findFIrst if you want to show a single result.



8.8k
edited Jun '18

Thanks

Actually my understanding of terminology is still in early stage. So ResultSet means, multiple results retrieved from database and will always have multilevel results even if its single set. And MODEL is actually one whole row from database ?

If i want it to iterage, how should I go in volt :

       {% for issues in issue.items %}

         <div class="card card-body">
        {{ items.id }}
        </div>
        {% endfor %}
</div>

And its not working.

edited Jun '18

Controller:

    $this->view->issues = $issues;

Template:

{% for issue in issues %}
         <div class="card card-body">
        {{ issue.id }}
        </div>
{% endfor %}


8.8k

Thanks ,

That helped.