排序模型关系日期

时间:2017-11-15 21:03:49

标签: php sorting model relationship phalcon

我有两张桌子:

Discussion:
╔════╦════════════════╗
║ id ║     name       ║
╠════╬════════════════╣
║  1 ║ Stackoverflow  ║
║  2 ║ Stackoverflow2 ║
╚════╩════════════════╝

Message:
╔════╦════════════════╦═══════════╦════════════╗
║ id ║ discussion_id  ║  content  ║    date    ║
╠════╬════════════════╬═══════════╬════════════╣
║  1 ║       1        ║ message 1 ║  jan 2011  ║
║  2 ║       1        ║ message 2 ║  jan 2012  ║
║  3 ║       2        ║ message 3 ║  jan 2013  ║
║  4 ║       2        ║ message 4 ║  jan 2014  ║
╚════╩════════════════╩═══════════╩════════════╝

Discussion::find()的结果应按如下方式排序:

Stackoverflow2   
Stackoverflow 

因为" Stackoverflow2"比#34; Stackoverflow"更新。根据{{​​1}}表中的日期。

你有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您的查询必须如下,

select discussion.id, discussion.name from Discussion join message on message.discussion_id = Discussion.id order by message.date

答案 1 :(得分:0)

谢谢,使用语法Phalcon,它变成了:

$discussions = $this->modelsManager->createBuilder()
->from('Phalcon\Model\DiscussionModel')
->join('Phalcon\Model\MessageModel')
->orderBy("Phalcon\Model\MessageModel.cts DESC")
->getQuery()
->execute();