CakePHP 1.3:hasMany条件显示不想要的记录

8

我有一个问题表,里面存储了很多问题。这些问题与问题主题相关,因此在问题和主题之间建立了“一对多”的关系。现在它看起来是这样的:

$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的记录,而不是第二个。我该怎么做?
2个回答

12

不要使用hasMany,像这样使用hasOne

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

请使用 hasOne 来完成此操作。 - Harman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接