Search code examples
phpsortingmodelrelationshipphalcon

Sort model relationship date


I have two tables:

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  ║
╚════╩════════════════╩═══════════╩════════════╝

The results of Discussion::find() should be sorted like this:

Stackoverflow2   
Stackoverflow 

Because "Stackoverflow2" is more recent than "Stackoverflow" according to the dates in the Message table.

Do you have an idea?


Solution

  • Your query must be like following,

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