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

Sort data by relative model count

I have a comment table:

Comment Table : | id | post_id | ...... |

Post Table: | id | catalog_id | ...... |

Catalog Table: | id | post_id |

so, in my Post Model:

 $this->hasMany('id','Model\Comment','post_id',['alias'=>'Comment']);

in my Comment Model:

$this->belongsTo('post_id','Model\Post','id',['alias'=>'Post']); 

what I want to implement is get the most comment Catalog NOT most comment Post.

with

paginator = new \Phalcon\Paginator\Adapter\Model([
            'data'       => $data,
            'limit'      => $this->recordPerPage,
            'page'       => $page,
        ]);

But I really don't know how sort the data by the sum of another table in phalcon.



5.2k

Can Phalcon can implement something like that?

I guess you need to have your data sorted before passing for pagination, Create a custom query to retrieve Posts by the count of Comments and then pass the retrieved data to paginator https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html

Or i didn't get you well.