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

Multiple joins and Volt

Hey,

this is about some sort of forum. I have 3 tables:

  • forum
  • user
  • forumRead

These are linked properly via the models. (forumRead [two belongs to] and forum + user with [has many]). I do get the last 10 forums with:

$this->view->forum = Forum::find(array("status != 'in_creation' AND status != 'closed'", "order" => "id DESC", "limit" => "10"));

Now I would like to mark the unread ones. The forum table does include a updated_at timestamp and the forumReadAt the list time the user visited that specific forum.

In volt I can go either from the forum or user perspective. In case of forum perspective:

{% if forum.updated_at > forum.forumRead.ticket_read_at %} unread {% endif %}

Of course this doesn't know as there is more than one user. How can implement the user using the volt syntax?

{% if forum.updated_at > forum.forumRead.[WHERE user_id = user.id].ticket_read_at %} unread {% endif %}

Or would you manage to do it? If possible I would like to avoid iterating through all users or tickets.



4.5k

Why can't you have that logic in the controller? And then pass variable called "unread" or something like that?

Then it would be as simple as {% if unread %}unread {%endif%}



5.7k

yeah, I coulld also write a normal query to include everything from start up without the object stuff. Mhh, so it isn't possible with vault and should be handled on controller side?

If I do so, would it be better to write the complete query instead of Forum::find(array(...)) or what kind of good object orientated solution would there be too?



33.8k

mirago has the point here. Do the search (logic) in the controller. Volt is for generating the views, and it doesn't make much sense doing SQL there.

And for getting the unreaded ones, you can do it with PHQL ( https://docs.phalcon.io/es/latest/reference/phql.html ) or with $forum->getForumRead(), and from then select only the ones that updated_at = NULL.