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.

Beginner question about volt and controllers

Hi,

I just started with Phalcon and coding and I am stuck with a little problem. I am trying to count objects in my view but I am unable to do so, so I ask for help here.

Basically I have 3 models : Series, Episodes and Subtitles.

Relations are as follow :

Series :

$this->hasMany('id', 'Episodes', 'series_id', array('alias' => 'Episodes'));

Episodes:

$this->hasMany('id', 'Subtitles', 'episodes_id', array('alias' => 'Subtitles'));
$this->belongsTo('series_id', 'Series', 'id', array('alias' => 'Series'));

Subtitles :

$this->belongsTo('episodes_id', 'Episodes', 'id', array('alias' => 'Episodes'));

In my SeriesController :

        $series = Series::find();
        $this->view->series = $series;

Then in the view I display series episodes like this :

{% for serie in series %}
                            {% for episode in serie.Episodes %}
                                {{ episode.title }}
                            {% endfor %}
{% endfor %}

What I want is episode count per serie. I am unable to find a way to do so.

Thanks for your help.



46.8k
Accepted
answer

I fixed your post for better layout. Look at the source to see what I changed.

I think that what you are looking for might be as simple as {{ serie.Episodes.count() }}.



419

First of all, thanks for your quick answer and post edition, this is really readable that way. I will do that next time.

Also, one more question about this. Would that work also for subtitles ? :

{{ serie.Episodes.Subtitles.count() }}


46.8k

No you would need to iterate on Episodes and then call episode.Subtitles.count().