我試圖返回相關聯的所有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