2011-12-06 58 views
0

我有以下cakephp綁定關係設置。他們正在尋找,但如何獲得嵌套在結果中的評論用戶記錄?cakephp如何做一個嵌套連接

 $this->Posts->bindModel(array(
     'hasOne' => array(
      'User' => array(
       'foreignKey' => false, 
       'type' => 'INNER', 
       'conditions' => array('Posts.user_id = Users.id') 
      ) 
     ), 
     'hasMany' => array(
      'Comment' => array(
       'foreignKey' => 'post_id', 
       'conditions' => array('Comment.active' => 1) 
      ) 
     ) 
    )); 

這個偉大工程,以得到這樣的結果:

[1] => Array 
    (
     [Posts] => Array 
      (
       [title] => test post 
       [body] => test body 
       [published] => 
       [id] => 15 
      ) 

     [User] => Array 
      (
       [id] => 7 
       [username] => admin 
       [password] => d0557b9de8bb6f7fb3248a017c7b67a6 
       [email] => [email protected] 
       [group_id] => 1 
       [created] => 2011-11-21 15:19:09 
      ) 

     [Comment] => Array 
      (
       [0] => Array 
        (
         [id] => 10 
         [user_id] => 7 
         [post_id] => 15 
         [text] => testdfasdfdsfasdfasdfasdfasd 
         [active] => 1 
         [created] => 2011-12-02 20:50:57 
         [published] => 2011-12-05 13:58:25 
        ) 

       [1] => Array 
        (
         [id] => 11 
         [user_id] => 7 
         [post_id] => 15 
         [text] => this is a test comment 
         [active] => 1 
         [created] => 2011-12-02 21:31:56 
         [published] => 2011-12-03 11:34:32 
        ) 

      ) 

    ) 

我的問題是怎麼也得上評論相關的用戶?有沒有辦法在我的查詢中嵌套評論和用戶之間的唯一關係?

回答

0

你可以在bindModel中使用contains嗎?從來沒有嘗試過...但值得一試。

$this->Posts->bindModel(array(
     'hasOne' => array(
      'User' => array(
       'foreignKey' => false, 
       'type' => 'INNER', 
       'conditions' => array('Posts.user_id = Users.id') 
      ) 
     ), 
     'hasMany' => array(
      'Comment' => array(
       'foreignKey' => 'post_id', 
       'conditions' => array('Comment.active' => 1), 
       'contain' => array('User'), 
      ) 
     ) 
    )); 

其實現在我讀了你的問題,我不確定你想要什麼。你想要這個人的評論者的user_id嗎?你能否詳細說明你想要多一點。