2012-10-06 37 views
1

我有模型Page和Commit。頁面有許多提交。 但有時我需要爲頁面獲取最後一次提交,有時需要獲取頁面提交的歷史記錄(最後20個或全部)。ActiveRecord&PHP:定義兩個模型之間不同關聯的最佳方式

我寫了模型驗證碼:

class Page extends ActiveRecord\Model { 
    static $has_many = array(
     array('commits', 
      'select'=> 'content', 
      'order' => 'id DESC', 
      'limit' => 1 
     )); 
} 
class Commit extends ActiveRecord\Model { 
    static $belongs_to = array(
     array('page')); 
} 

所以我需要做的有顯示器的機會所有的提交([「限制」 => 20]前。)?

回答

0

這有點一種解決辦法,但是這應該這樣做...有點兒從Rails的模擬示波器:

class Page extends ActiveRecord\Model { 
    static $has_many = array(
     array('limited_commits', 
      'class_name' => 'Commit', 
      'select'=> 'content', 
      'order' => 'id DESC', 
      'limit' => 1 
     ), 
     array('all_commits', 
      'class_name' => 'Commit', 
      'select' => 'content', 
      'order' => 'id DESC' 
     ) 
    ); 
} 
class Commit extends ActiveRecord\Model { 
    static $belongs_to = array(
     array('page')); 
} 

,然後只用了「範圍」,您需要:

Page::first->limited_commits 
Page::first->all_commits 

這不是一個真正的整體解決方案,但它應該做的伎倆...

+0

這有助於!非常感謝! – mikatakana

相關問題