hasMany condition show unwanted records cakephp 1.3
Asked Answered
H

2

8

I have Questions table which have store so many question. Question related to question_topics so have create has many relationship with question. Now it's look 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 look like this

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 want first record only which have Question topic id 167 not second one. How can i do this.

Hawking answered 24/4, 2014 at 12:13 Comment(0)
I
12

Don't use hasMany, Use has one like this

$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'))))); 
Irresoluble answered 24/4, 2014 at 12:34 Comment(0)
T
0

Something like this?

$question = $this->Question->find('first', array('conditions' => array('QuestionTopic.id' => 167));
Tertia answered 24/4, 2014 at 12:31 Comment(1)
Please hasOne to do thisHawking

© 2022 - 2024 — McMap. All rights reserved.