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

volt foreach not printing

hello all, I am using invo

  1. I wrote this code in my controller

    $posts = Posts::find()->**toArray**();         
    $**this**->view->posts = **$posts**;
  2. and this in my view

    {%for post in posts%} {{post['user_id']}} {%endfor%}

I get a pretty much blank page with the navbar and footer

am I doing something wrong?

Looks pretty good. What does it look like when you print_r() $posts in the controller?

Also, as an aside - why are you running your resultset through toArray()? Looking at your Volt code - you could just as easily use object syntax, and you wouldn't have the overhead of the translation.



43.9k
Accepted
answer

Hi,


// in controller:
$posts = Posts::find();         
$this->view->posts = $posts;

// in view:
{%for post in posts%} {{ post.user_id }} {%endfor%}

hello quasipickle, you said 'What does it look like when you print_r() $posts in the controller?'

it dumps out all the class info for

Phalcon\Mvc\Model\Resultset\Simple Object

and the toArray() makes it print out

Array ( [0] => Array ( [post_id] => 1 [user_id] => 1 [text] => hi ) [1] => Array ( [post_id] => 2 [user_id] => 1 [text] => hello ) ) 1

what is object syntax?

Looks pretty good. What does it look like when you print_r() $posts in the controller?

Also, as an aside - why are you running your resultset through toArray()? Looking at your Volt code - you could just as easily use object syntax, and you wouldn't have the overhead of the translation.

hello le51,

your code looks like what I was trying to do my view dumps out Posts Object ( [post_id] => 1 [user_id] => 1 [text] => hi and alot more

I see that the results are objects,

but i am confused when i use print_r($post);

i get Array ( [post_id] => 1 [user_id] => 1 [text] => hi ) 1

could it be a problem with invo?

I am very confused.

Hi,


// in controller:
$posts = Posts::find();         
$this->view->posts = $posts;

// in view:
{%for post in posts%} {{ post.user_id }} {%endfor%}


43.9k

// in controller:
$posts = Posts::find();         
$this->view->posts = $posts;

// in view:
{%for post in posts%} {{ post.user_id }} {%endfor%}

outputs "Array ( [post_id] => 1 [user_id] => 1 [text] => hi )" ???

sorry about that, I changed the $posts = Posts::find(); to $posts = Posts::find()->toArray(); because the $posts = Posts::find(); was dumping out class info for Phalcon\Mvc\Model\Resultset\Simple Object


// in controller:
$posts = Posts::find();         
$this->view->posts = $posts;

// in view:
{%for post in posts%} {{ post.user_id }} {%endfor%}

outputs "Array ( [post_id] => 1 [user_id] => 1 [text] => hi )" ???

I can't believe it...

it was all the layout's fault added <div align="center"> {{ content() }} </div> to my layout file...

everything works

sorry figured it out

you were right le51

// in controller: $posts = Posts::find();
$this->view->posts = $posts;

// in view: {%for post in posts%} {{ post.user_id }} {%endfor%}

works

thank you everyone



43.9k

you are using an ORM to acces and modify your db records, so

  • $posts = Posts::find(); will return a object resultset of Posts objects,
  • while $post = Posts::findByPk('18'); will return a single object of class Posts.

Phalcon's MVC structure (like many other mvc framework) allows you to pass these entities from the controller to the views and, afer that, to access to their attributes in a simple way without the hassle of object to array conversion.

Did you try the code in the previous message ?



43.9k

good.

Hope you will enjoy phalcon !

thank you for your help!