duanjia9577 2016-03-18 09:39
浏览 37

CakePHP模型关联,条件不起作用

I have Three tables Projects, Tasks and Tags. Projects.id is the primary key of the first table, Tasks.id is the PK of the second table and Tags.id is the PK of Third table.

Projects Model Code snippet

public $primaryKey = 'id';
public $hasMany = array(
    'Tasks' => array('className' => 'Tasks','foreignKey' => 'project_id')
); 

Tasks Model Code snippet

public $primaryKey = 'id';
public $hasMany = array(
    'Tags' => array('className' => 'Tags','foreignKey' => 'task_id')
);

The below query return some unexpected result.

$data = $this->Projects->find('all', array(
            'recursive' => 2,
            'contain' => array(
                'Tags' => array(
                    'conditions' => array('Tags.tag_name =' => "Driver")
                 )
            )
 ));

The response data i am getting is

 [Projects] => Array
                (
                    [id] => 1
                    [project_id] => 1234
                    [project_name] => XYZ
                )

            [Tasks] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [project_id] => 1
                            [task_id] => 12
                            [task_name] => task1
                            [Tags] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1
                                            [task_id] => 1
                                            [tag_id] => 3444
                                            [tag_name] => Driver
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 2
                            [project_id] => 1
                            [task_id] => 343242
                            [task_name] => task2


                            [Tags] => Array
                                (
                                )

                        )

See i am even getting tasks without tag as Driver. How i can exclude tasks without tag Driver?

In another word task1 do have Tag as Driver, so that should be there. But task2 don't have any tag, even then the return data contain task2. How to fix it?

  • 写回答

1条回答 默认 最新

  • duancheng3042 2016-03-18 13:26
    关注

    You cannot limit the results of the main or parent model based on conditions on child models because "contain" actually creates separate queries for each contain model.

    To get the proper result you must use join.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度