2013-03-11 2 views
0

다음 모델과 연결이 있습니다.Cakephp 순환 연결이 표시되지 않음

질문 (모델) (클래스 명 = AnswerChoice) AnswerChoice (모델) 많은 AnswerChoices을 가지고 (여기서 클래스 = '질문')

따라서는, FollowUpQuestion 많은 AnswerChoices을해야 하나 FollowUpQuestion이 있습니다.

하지만 결과는 FollowUpQuestion에서 중지 질문 객체 또는 질문 목록을 읽으려고 할 때. FollowChoices가 FollowUpQuestion에 표시되지 않습니다.

가 나는 3, 4, 5 ...하지만에 소용 $ this-> 모델 -> 재귀를 설정했다.

왜 이런가요? 이 순환 연결은 CakePHP에서 허용되지 않습니까? 아니면이 기능이 작동하도록 설정해야하는 특수 설정이 있습니까?

SQL 스크립트

NOT NOT NULL로 device_questions ( id INT (11)이있는 경우 테이블 만들기, parent_answer_id INT (11) NULL NOT, text 텍스트 NULL NOT, answer_type ENUM ('다중 선택 ','무료 양식 ') NULL NOT, device_question_group_id INT (11) NULL NOT, status ENUM ('활성 ','활동 ',', ' create_date 날짜를 NOT') NOT NULL DEFAULT '를 삭제 활성 NUL L, modified_date datetime NOT NULL, PRIMARY KEY (id) ) ENGINE = InnoDB DEFAULT CHARSET = utf8;

INSERT device_questions (id, parent_answer_id, text, answer_type, device_question_group_id, status, create_date, modified_date가) 값으로 (29, 0, '테스트 멀티 레벨 질문', '다중 선택', 7, '활동', ' (30, 33, 'Why no?', 'Multiple Choice', 7, 'Active', ' 2013-03-11 01:25:44 ','2013-03-11 01:25:44 '), (31, 36,'왜 건너 뛰셨습니까?','Free Form ', 7,'Active ','2013-03-11 01:25:44 ','2013-03-11 01:25:44 '); NOT은 NULL NOT NULL NOT device_question_answer_choices ( id INT (11), device_question_id INT (11)이있는 경우

text 텍스트 NULL NOT, 테이블 만들기, has_follow_up TINYINT (1) DEFAULT NULL의 create_date 날짜 NULL NOT, modified_date datetime DEFAULT NULL, 기본 키 (id) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; device_question_answer_choices (id, device_question_id, text, has_follow_up, create_date, modified_date)는 (32, 29, '예'0 '2013년 3월 11일 1시 25분 43초', '2013-03을 VALUES

INSERT INTO -11 01:25:43 '), (33, 29,'아니요 ', 1,'2013-03-11 01:25:44 ','2013-03-11 01:25:44 '), (34, 30, 'Just coz', 0, '2013-03-11 01:25:44', '2013-03-11 01:25:44'), (35, 30, '그게 '0,'2013-03-11 01:25:44 ','2013-03-11 01:25:44 '), (36, 30,'건너 뛰기 ', 1,'2013- 03-11 01:25:44 ','2013-03-11 01:25:44 '), (37, 29,'건너 뛰기 ', 0,'2013-03-11 01:25:44 ',' 2013-03-11 01:25:44 '); `

.

모델

애플리케이션 :: 용도 ('AppModel이 ","모델 ");

클래스 DeviceQuestion는 AppModel이 {

public $hasMany = array(
    'AnswerChoices' => array(
     'className' => 'DeviceQuestionAnswerChoice', 
     'foreignKey' => 'device_question_id', 
     'dependent' => false, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
    ) 
); 

}을 확장

애플리케이션 :: 용도 ('AppModel이 ","모델 ");

클래스 DeviceQuestionAnswerChoice는 AppModel이 {

public $hasOne = array(
    'FollowUpQuestion' => array(
      'className' => 'DeviceQuestion', 
      'foreignKey' => 'parent_answer_id', 
      'dependent' => false, 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
    ) 
); 

}

을 확장합니다.

컨트롤러

클래스 DeviceQuestionsController는 AppController가 {

var $name = 'DeviceQuestions'; 

public $helpers = array('Js', 'Time'); 

var $uses = array('DeviceQuestion', 'DeviceQuestionAnswerChoice'); 

public function test() { 
    $this->DeviceQuestion->recursive = 3; 

    $question = $this->DeviceQuestion->read(null, 29); 
    $this->set('question', $question); // or echo debug($question); 
} 

}

을 연장한다.

출력. 참고 FollowUpQuestion 배열은 AnswerChoices 배열을 표시하지 않습니다 (줄 29).

사전에 http://pastebin.com/GxA7yzs1

감사를 참조하십시오.

답변

0

해결!

public $hasOne = array(
    'FollowUpQuestion' => array(
     'className' => 'DeviceQuestion', 
     'foreignKey' => 'parent_answer_id', 
     'dependent' => false, 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
) 
); 

public $hasMany = array(
    'FollowUpQuestion' => array(
     'className' => 'DeviceQuestion', 
     'foreignKey' => 'parent_answer_id', 
     'dependent' => false, 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
) 
); 

이 지금 내 후속 질문에 대한 답변 선택을 반환합니다. 5 레벨로 이동하려면 재귀를 5로 설정해야했습니다.

나는 결코 하나 이상을 가질 수는 없지만 FollowUpQuestion 배열의 첫 번째 인덱스를 가져와야합니다. 변경 후

출력 : http://pastebin.com/j4JVfkCB

0

당신은 아마 제대로 여부 FollowUp_Question 별명이 실제로 Question 모델하지만 오히려 AppModel 인스턴스를 사용하지 않는할지 인해 규칙에 따라 모델 클래스 이름/파일 이름을라는 이름의 단체를 구성하지 않았습니다. 이것은 CakePHP 기능입니다. 모델에 적합한 파일을 찾을 수 없으면 AppModel을 사용하고 규칙에 따라 사용할 테이블을 설정합니다.

또한 규칙에 따라 모델의 이름은 CamelCasedSingular 그래서 당신의 Answer_Choices 대신 FollowUp_QuestionFollowUpQuestion해야한다 DB 테이블 answer_choices 및 별명 AnswerChoice해야 있어야한다.

+0

감사 ADmad. 당신이 만든 컨벤션 추천서는 제가 실제로 수업을받는 방식입니다. 나는 그 질문에 대해 잘못 언급했다. 오해의 소지가있는 정보에 대해 사과드립니다. 나는 초기 게시물을 수정했습니다. FollowUpQuestion 별칭에 관한 첫 번째 권장 사항은 질문 모델을 사용하지 않습니다 ... 저는 실제로 AnswerChoices에 대한 연결이 아닌 질문 정보를 얻고 있습니다. –

+0

아마도 모델을 보여주는 코드와 데이터를 가져 오기 위해 사용하는 찾기 호출을 공유하십시오. – ADmad

+0

SQL 스크립트를 포함한 모든 관련 코드를 추가했습니다. –

관련 문제