加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

hasMany条件显示不需要的记录cakephp 1.3

发布时间:2020-12-13 18:10:58 所属栏目:PHP教程 来源:网络整理
导读:我有问题表,存储了这么多问题.与question_topics相关的问题因此创建与问题有很多关系.现在它看起来像这样: $this-Question-bindModel( array( 'hasMany' = array( 'QuestionTopic'=array( 'className' = 'QuestionTopic','foreignKey' = 'question_id','dep
我有问题表,存储了这么多问题.与question_topics相关的问题因此创建与问题有很多关系.现在它看起来像这样:
$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而不是第二个.我怎样才能做到这一点.

不要使用hasMany,使用有这样的一个
$this->Question->bindModel(
                array(
                    'hasOne' => array(
                        'QuestionTopic'=>array(
                            'className' => 'QuestionTopic','type' => 'left'
                        )
                    )
                )
        );
        print_r($this->Question->find('all',array('conditions'=>array('QuestionTopic.areas_id' => array('165')))));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读