我试图返回相关联的所有features
和所有的(如果有的话)features_user_types
其中user_type_id =?直接运行。
因此,例如,我有2 features
。只要user_type_id = 2,我希望两者都与所有关联的features_user_types
一起返回。如果没有匹配的feature_user_type
,则无论如何它都应该返回feature
。
预期结果
示例输出:WHERE user_type_id = 2
"features": [
{
"id": 1,
"features_user_types": [
{
"id": 79,
"feature_id": 1,
"user_type_id": 2,
"position": 3
}
]
},
{
"id": 2,
"features_user_types": []
}
]
实际结果
然而,目前它返回所有相关联的features_user_types尽管它们的ID不等于2。
$ q uery->指定者()输出:
"features": [
{
"id": 1,
"features_user_types": [
{
"id": 79,
"feature_id": 1,
"user_type_id": 2,
"position": 3
}
]
},
{
"id": 2,
"features_user_types": [
{
"id": 72,
"feature_id": 2,
"user_type_id": 1,
"position": 9
}
]
}
]
数据结构
表结构:
features
-id
features_user_types
-id
-feature_id
-user_type_id
-position
user_types
-id
CakePHP的协会定义:
FeaturesTable:
$this->belongsToMany('UserTypes', [
'foreignKey' => 'feature_id',
'targetForeignKey' => 'user_type_id',
'joinTable' => 'features_user_types'
]);
$this->hasMany('FeaturesUserTypes', [
'foreignKey' => 'feature_id'
]);
UserTypesTable:
$this->belongsToMany('Features', [
'foreignKey' => 'user_type_id',
'targetForeignKey' => 'feature_id',
'joinTable' => 'features_user_types'
]);
$this->hasMany('FeaturesUserTypes', [
'className' => 'FeaturesUserTypes',
'foreignKey' => 'user_type_id'
]);
FeaturesUserTypesTable:
$this->belongsTo('Features', [
'foreignKey' => 'feature_id',
'joinType' => 'INNER'
]);
$this->belongsTo('UserTypes', [
'foreignKey' => 'user_type_id',
'joinType' => 'INNER'
]);
查询对象
我根据$query->sql()
在被创建下列SQL我的CakePHP应用程序查询生成器:
SELECT DISTINCT
Features.id AS `Features__id`,
Features.created AS `Features__created`,
Features.modified AS `Features__modified`,
Features.name AS `Features__name`,
Features.description AS `Features__description`
FROM features Features
LEFT JOIN features_user_types FeaturesUserTypes
ON (FeaturesUserTypes.user_type_id = 2
AND Features.id = (FeaturesUserTypes.feature_id))
MySQL的
但是,如果我直接复制并粘贴到MySQL这个我得到我期望的结果,所有features
只有featurs_user_types
匹配的ID被返回。
实际查询:
SELECT DISTINCT *
FROM features Features
LEFT JOIN features_user_types FeaturesUserTypes
ON (FeaturesUserTypes.user_type_id = 2
AND Features.id = (FeaturesUserTypes.feature_id))
MySQL的输出:
----------------------------------------------------------------------------
|ID (feature id)|ID (feature_user_type_id)|feature_id|user_type_id|position|
| 1 | 79 | 1 | 2 | 3 |
| 2 | NULL | NULL | NULL | NULL |
----------------------------------------------------------------------------
CODE
的AppController:
我AppController中是非常通用的,但建把它从URL中paramters生成并执行sql q ueries。它是一个相当大的文件,所以我使用调试器进行了检查,并记录了$ query被更改并填充到变量中的所有行,以使其更加明显。
$key = 'FeaturesUserTypes.user_type_id';
$value = 2;
$model = $this->loadModel();
$query = $model->find('all', ['fields' => $this->getFields()]);
$query->contain(['FeaturesUserTypes']);
$query->leftJoinWith('FeaturesUserTypes', function($q) use ($key, $value) {
return $q->where([$key => $value]);
});
$query->distinct();
$results = $query->toArray();
关于可能发生什么的任何想法?我正在运行CakePHP 3和PHP 5.6.10。谢谢!
查询将返回来自'features'表的所有**记录,而不仅仅是匹配的记录。这个查询不太可能粘贴到MySQL中,只会返回匹配的记录。如果只需要匹配记录,则将左连接更改为内连接。 – Shadow
该查询在连接中检查了id,它在MySQL中完全按预期工作。结果绝不符合CakePHP的结果。这种类型的查询适用于其他模型的CakePHP,因此我认为它可能与关系类型有关。 – jrquick
不,它也会返回MySQL中的所有结果。请参阅下面提供的答案。 – Shadow