2011-12-06 4 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를 원하십니까? 당신이 원하는 것을 조금 더 자세히 설명 할 수 있습니까?