首页 > 解决方案 > 对 hasMany 和 BelongsToMany 模型使用 **joinType** 的替代方法

问题描述

我想获得左侧模型/表格的结果,当且仅当条件在右侧/包含模型中满足 hasMany 和 BelongsToMany 关联。

据我所知,joinType 不适用于hasManyBelongsToMany关联,因此我无法使用InnerJoin ...

这是我的查询,

$this->loadModel('Logins');
        $x = $this->Logins->find('all', [
            'fields' => ['id', 'user_name'],
            'contain' => [
                'Sites' => ['conditions' => ['site_id' => 10]]
            ],
        ]);
        pr($x->enableHydration(false)->toArray());
        die;

这就是结果,

[0] => Array
        (
            [id] => 13
            [user_name] => Mal123
            [sites] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 14
            [user_name] => rrc
            [sites] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [site_name] => Smelter
                            [site_location] => NA
                            [client_id] => 1
                            [site_status_id] => 1
                            [_joinData] => Array
                                (
                                    [id] => 15
                                    [login_id] => 14
                                    [site_id] => 10
                                )

                        )

                )

        )

问题:-

  1. 为什么hasManyBelongsToMany关联没有“joinType”选项?
  2. 我怎样才能存档?我只想得到第 1 个而不是第 0 个

标签: cakephpcakephp-3.0cakephp-3.xcakephp-3.4

解决方案


推荐阅读