I am building a comment, with replies system using CakePHP and Backbone. But I am having some issues with my CakePHP query, that does not order in the way it needs to be.
So this is database system I have right now,
Posts
ID (Auto, PK)
MainPost (Long Text)
Timestamp
User_id
Comments
ID (Auto, PK)
SubComment (Long Text)
Timestamp
User_id
Post_id (FK, linking to the Posts table)
Now I think my model is ok (if needed I can post that code). I can save and update the posts and comments. But what I need to when a new comment is added to the system. That whole post is moved to the top.
This is the query I have done on my Posts
Table,
$Table->find('all')
->contain(['Comments',
'Comments.Users',
])
// ->order(['Posts.Comments.timestamp' => 'DESC'])
// ->matching('Comments', function ($q) {
// return $q->where(['Comments.timestamp' => 'DESC']);
// })
->toArray();
So I want to do a sort, like my order looks above, I basically want to order my Posts Table based on the timestamp of my Comments Table.
When I run this query, with order enabled, it just says that "Unknown column" but I don't know why? I have a LeftJoin
in my model linking the Comments to my Posts table.
I have tried doing sorts
and orders
within the contain
call of the query, however these just sort/order the comments and not the posts!
So what am I doing wrong?