Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 ?



28.4k

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.



1.2k
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?



1.2k

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?



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



28.4k

You may accept Videles answer so that thread appears as solved