HasMany condition show unwanted cakephp 1.3 entries

I have a question table that stores so many questions. The question is related to question_topics, so it has a lot of relationship with the question. Now it looks like this:

$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; 

When I saw the result, then it looks like

 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 ( ) ) ) 

I only need the first entry, which has a question with question id 167, not the second. How can i do this.

+7
cakephp has-many
source share
2 answers

Do not use hasMany, use one such

 $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'))))); 
+11
source share

Something like that?

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

All Articles