2014-04-24 63 views
7

不需要的記錄我有問題表,店內有這麼多的問題。有關question_topics等有創建問題與問題一對多的關係。現在,它看起來像這樣:的hasMany條件顯示CakePHP的1.3

$this->Question->bindModel(
     array(
      'hasMany' => array(
       'QuestionTopic'=>array(
        'className' => 'QuestionTopic', 
        'foreignKey' => 'question_id', 
        'dependent' => true, 
        'conditions' => array('QuestionTopic.areas_id' => 165), 
        'type' => 'left' 
       ) 
      ) 
     ) 
); 
print_r($this->Question->find('all')); 
die; 

當我看到結果,那麼它看起來像這樣

Array 
(
    [0] => Array 
     (
      [Question] => Array 
       (
        [id] => 89 
        [user_id] => 1 
        [question_group_id] => 0 
        [question] => jQuery function here 
        [target_id] => 1 
        [type] => 1 
        [order] => 1 
        [description] => additional info here 
        [privacy_friend_id] => 
        [channel_id] => 1 
        [status] => 0 
        [location] => Chandigarh, India 
        [regions] => 307 
        [select_country] => 381 
        [select_states] => 515 
        [created] => 2014-04-15 06:59:44 
        [modified] => 2014-04-15 06:59:44 
       ) 

      [QuestionTopic] => Array 
       (
        [0] => Array 
         (
          [id] => 167 
          [areas_id] => 165 
          [question_id] => 89 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [Question] => Array 
       (
        [id] => 90 
        [user_id] => 1 
        [question_group_id] => 0 
        [question] => Art 
        [target_id] => 2 
        [type] => 1 
        [order] => 1 
        [description] => addional infomation here 
        [privacy_friend_id] => 
        [channel_id] => 1 
        [status] => 0 
        [location] => Chandigarh, India 
        [regions] => 307 
        [select_country] => 381 
        [select_states] => 515 
        [created] => 2014-04-15 07:52:17 
        [modified] => 2014-04-15 07:52:17 
       ) 

      [QuestionTopic] => Array 
       (
       ) 

     ) 
) 

我想先只記錄其中有問題主題ID 167沒有第二個。我怎樣才能做到這一點。

回答

11

不使用的hasMany,使用了一個類似這樣的

$this->Question->bindModel(
       array(
        'hasOne' => array(
         'QuestionTopic'=>array(
          'className' => 'QuestionTopic', 
          'foreignKey' => 'question_id', 
          'dependent' => true, 
          'type' => 'left' 
         ) 
        ) 
       ) 
     ); 
     print_r($this->Question->find('all',array('conditions'=>array('QuestionTopic.areas_id' => array('165'))))); 
0

像這樣的事情?

$question = $this->Question->find('first', array('conditions' => array('QuestionTopic.id' => 167)); 
+2

錯讓這將不會工作。 –

+0

請hasOne做到這一點 – Harman