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 count relationship

I would like to know how to call the count function in volt?

$robot = Robots::findFirst(2); echo "The robot has ", $robot->countRobotsParts(), " parts\n";

But when i try {{ robot.countRobotsParts() }} it's not working? How can I achieve this ?



43.9k

Hi,

you must pass $robot to the view ? If yes, that's strange ..., do you get any relevant error message ?

Note: Usually it is considered to be best pratice to prepare all you data in the controller context and then pass them to the view.



13.8k
edited Mar '17

What do you get if you dump the robotparts? Does it return anything?

If I set view->disable() and make a foreach, and call the $robot->countRobotsParts(), it works perfect.

But if I pass the virable to the view, and in volt call robot.countRobotsParts() it dosent work?



13.8k

Can you paste your code? Is a bit easier to help if its possible to see the actual code (controller action / .volt)

$this->hasMany('id', NAMESPACE.'\BlogsPostsCommentsReplies', 'commentid', array( 'alias' => 'replies', 'params' => [ 'order' => 'id', 'limit' => 12 ] ));

Then in my controller:

    $comments = BlogsPostsComments::find();
    $this->view->comments = $comments;

And finally in my View:

{% for comment in comments %}
    {{ comments.countReplies() }}
{% endfor %}

But it is not working?



13.8k
Accepted
answer
edited Mar '17

Theres a typo in your .volt comments. should be comment. as below.


{% for comment in comments %}
    {{ comment.countReplies() }}
{% endfor %}
edited Mar '17

Oh god, thanks!

It's working fine now!



43.9k

You may accept Videles answer so that thread appears as solved