2012-09-07 2 views
0

그래서 CakePHP를 사용하는 기본 회원 시스템이 있습니다. 그들은 물건을 게시 할 수 있고 코멘트 시스템을 추가하고 싶습니다.의견에서 추가 정보 얻기

은 그래서 DB의 테이블을 만들어 모델 및 사용자/글/댓글이 방식으로 연결되어 만들어진 :

사용자 모델 :

public $hasMany = array("Post", "Comment"); 

게시물 모델 :

public $belongsTo = "User"; 
public $hasMany = "Comment"; 

의견 모델 :

public $belongsTo = array("User", "Post"); 
I'ld 더 좋아

array(
    'Post' => array(
     'id' => '25', 
     'title' => 'Rocket Club', 
     'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam elementum sem ac sem imperdiet cursus. Quisque venenatis pulvinar ornare. Donec rutrum, lacus vel imperdiet sagittis, metus risus interdum ante, iaculis venenatis arcu nibh et odio.', 
     'image' => '25-75da9db5.jpg', 
     'created' => '2012-09-07 01:40:14', 
     'user_id' => '19' 
    ), 
    'User' => array(
     'password' => '*****', 
     'id' => '19', 
     'username' => 'Axiol', 
     'mail' => '*****', 
     'firstname' => 'Arnaud', 
     'lastname' => 'Delante', 
     'description' => 'C'est moi le monsieur qui est derrière ce site. Dingue hein ?', 
     'local' => '4420 Saint-Nicolas, Belgique', 
     'twitter' => 'Axiol', 
     'facebook' => 'axiol', 
     'gplus' => '100906827397700671747', 
     'github' => 'Axiol', 
     'website' => 'http://www.axioland.me', 
     'created' => '2012-09-04 02:10:31', 
     'lastlogin' => '2012-09-07 01:38:44', 
     'active' => '1' 
    ), 
    'Comment' => array(
     (int) 0 => array(
      'id' => '1', 
      'content' => 'Test test d'un joli commentaire constructif et tout pour aider la personne qui a postée l'image.', 
      'post_id' => '25', 
      'user_id' => '19' 
     ) 
    ) 
) 

을하지만 :

가 그리고 그것은 꽤 잘 작동하는 것 같다, 나는 제대로 후 여기, 게시물에 대한 디버그 코멘트를 얻을. 내가 닉네임과 그의 메일과 같은 코멘트를 작성한 사용자에 대한 추가 정보를 쉽게 얻을 수 있는지 알고 싶습니다. 거기에 빌드가 있습니까?

답변

2

두 가지 방법이 있습니다.

1- 세트 'recursive' => 2find()에 전화하십시오. 이 접근 방식의 문제점은 두 번째 수준의 연결에서 모든 데이터를 가져 오는 것이므로 너무 많을 수 있습니다. 이 같은

2를 사용 Containable behavior, 시도 뭔가 :

$this->Post->find('first', array('conditions' => array('Post.id' => 1), 'contain' => array('User', 'Comment', 'Comment.User'))); 
+0

감사합니다. 그리고 너무 재귀에 대한 이야기 ​​주셔서 감사합니다, 나중에 도움이 될 수 :) – Axiol

+0

흠 ... 구문에 대한 확신합니까? 왜냐하면 내가보기에 보내는 것이기 때문에 : $ this-> set ("post", $ this-> Post-> find ("first", array ("conditions"=> array ("Post.id"=> $ id), "contains"=> array ("User", "Comment", "Comment.User"))))); 그리고 디버그에서 나는 주석 이외에 아무것도 가지고 있지 않습니다./ – Axiol

+0

Post 동작 모델에 Containable Behavior를 첨부 했습니까 ?? – Choma