我正在建立评论,回复系统使用CakePHP和Backbone。但是我的CakePHP查询存在一些问题,它没有以它需要的方式排序。
所以这是我现在拥有的数据库系统,
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)
现在我认为我的模型没问题(如果需要,我可以发布该代码)。我可以保存并更新帖子和评论。但是当我向系统添加新评论时我需要什么。整个帖子都移到了顶部。
这是我在Posts
表上进行的查询,
$Table->find('all')
->contain(['Comments',
'Comments.Users',
])
// ->order(['Posts.Comments.timestamp' => 'DESC'])
// ->matching('Comments', function ($q) {
// return $q->where(['Comments.timestamp' => 'DESC']);
// })
->toArray();
所以我想做一个排序,就像上面的订单一样,我基本上想根据我的评论表的时间戳来订购我的帖子表。
当我运行此查询时,启用了订单,它只是说"未知列"但我不知道为什么?我的模型中有LeftJoin
将评论链接到我的帖子表。
我已尝试在查询的sorts
调用中执行orders
和contain
,但这些只是排序/排序评论而非帖子!
那么我做错了什么?