2015-02-05 82 views
0

我有兩種型號:事件和日期。一個事件有很多日期。我想按最近的日期排序。例如:Cakephp:按相關型號查找訂單

[0] => Array 
     (
      [Event] => Array 
       (
        [id] => 1 
        [name] => Event 1 
       ) 

      [Date] => Array 
       (
        [0] => Array 
         (
          [id] => 1 
          [date] => 2015-02-05 
          [start_time] => 06:00:00 
          [end_time] => 17:00:00 
          [evento_id] => 1 
         ) 
        [1] => Array 
         (
          [id] => 2 
          [date] => 2015-02-06 
          [start_time] => 06:00:00 
          [end_time] => 17:00:00 
          [evento_id] => 1 
         ) 

       ) 

     ) 
[1] => Array 
     (
      [Event] => Array 
       (
        [id] => 2 
        [name] => Event 2 
       ) 

      [Date] => Array 
       (
        [0] => Array 
         (
          [id] => 3 
          [date] => 2015-02-11 
          [start_time] => 06:00:00 
          [end_time] => 17:00:00 
          [event_id] => 2 
         )  
       ) 

     ) 
[2] => Array 
     (
      [Event] => Array 
       (
        [id] => 3 
        [name] => Event 3 
       ) 

      [Date] => Array 
       (
        [0] => Array 
         (
          [id] => 5 
          [date] => 2015-02-02 
          [start_time] => 06:00:00 
          [end_time] => 17:00:00 
          [event_id] => 3 
         ) 
        [1] => Array 
         (
          [id] => 6 
          [date] => 2015-02-03 
          [start_time] => 06:00:00 
          [end_time] => 17:00:00 
          [event_id] => 3 
         ) 

       ) 

     ) 

所以,我想要檢索的事件順序是:事件3,事件1,事件2

這是我的我打電話find方法:

$this->Event->find('all', array('contain' => array('Date' => array('conditions' => array('Date.date >=' => date('Y-m-d')))))); 

這是可能做到這一點與CakePHP的發現方法?謝謝。

+0

這不是通過'find'但[哈希::排序(http://book.cakephp.org/2.0/en/core-utility-libraries /hash.html#Hash::sort)可以使用。例如:將您的'find'賦值給$ results然後'$ resultsort = Hash :: sort($ results,'{n} .Date。{n} .date','asc');' – AgRizzo 2015-02-06 11:58:15

回答

0

像這樣的事情應該去做

$this->Event->find('all', array(
    'contain' => array(
     'Date' => array(
      'conditions' => array(' 
       Date.date >=' => date('Y-m-d') 
      ) 
     ) 
    ), 
    order => array(
     'COUNT(Date.id)' => 'DESC' 
    ) 

)); 
+0

我得到以下錯誤:錯誤:SQLSTATE [42S22]:未找到列:1054'訂單子句'中的未知列'Date.id'。 試圖將訂單移動到「包含日期」,但仍然給了我相同的訂單。 – 2015-02-05 22:55:21

+0

'order'應輸入爲'order',但我認爲'GROUP_BY'可能更合適 – 2015-02-05 23:55:28

+0

是的,我在測試時修正了'訂單'。 但問題是'Date.id'不是來自Event表的成員。如果我嘗試一個連接而不是一個包含什麼?它可能會有幫助嗎? – 2015-02-06 00:12:18