我在Model-> beforeFind($ queryData)中,尝试在具有belongsTo关联的模型上向queryData添加JOIN条件。不幸的是,新的JOIN引用了belongsTo关联中的一个表,所以它必须出现在查询中的belongsTo之后。
这是我的Tagged-> belongsTo协会:
app\plugins\tags\models\tagged.php (line 192)
Array
(
[Tag] => Array
(
[className] => Tag
[foreignKey] => tag_id
[conditions] =>
[fields] =>
[order] =>
[counterCache] =>
)
[Group] => Array
(
[className] => Group
[foreignKey] => foreign_key
[conditions] => Array
(
[Tagged.model] => Group
)
[fields] =>
[order] =>
[counterCache] =>
)
)
这是在Tagged-> beforeFind()中添加的JOIN,注意尚未添加belongsTo连接:
app\plugins\tags\models\tagged.php (line 194)
Array
(
[conditions] => Array
(
[Tag.keyname] => europe
)
[fields] => Array
(
[0] => DISTINCT Group.*
[1] => GroupPermission.*
)
[joins] => Array
(
[0] => Array
(
[table] => permissions
[alias] => GroupPermission
[foreignKey] =>
[type] => INNER
[conditions] => Array
(
[GroupPermission.model] => Group
[0] => GroupPermission.foreignId = Group.id
[or] => Array
( ... )
)
)
)
[limit] =>
[offset] =>
[order] => Array
(
[0] =>
)
[page] => 1
[group] =>
[callbacks] => 1
[by] => europe
[model] => Group
)
当我检查输出时,它在“on子句”中的“1054:未知列'Group.id'失败”,因为权限连接在组加入之前出现。
SELECT DISTINCT `Group`.*, `GroupPermission`.*
FROM `tagged` AS `Tagged`
INNER JOIN permissions AS `GroupPermission` ON (`GroupPermission`.`model` = 'Group' AND `GroupPermission`.`foreignId` = `Group`.`id` AND (...))
LEFT JOIN `tags` AS `Tag` ON (`Tagged`.`tag_id` = `Tag`.`id`)
LEFT JOIN `groups` AS `Group` ON (`Tagged`.`foreign_key` = `Group`.`id` AND `Tagged`.`model` = 'Group')
WHERE `Tag`.`keyname` = 'europe'
但是这个SQL(加入了Permissions的移动到最后)工作正常:
SELECT DISTINCT `Group`.*, `GroupPermission`.*
FROM `tagged` AS `Tagged`
LEFT JOIN `tags` AS `Tag` ON (`Tagged`.`tag_id` = `Tag`.`id`)
LEFT JOIN `groups` AS `Group` ON (`Tagged`.`foreign_key` = `Group`.`id` AND `Tagged`.`model` = 'Group')
INNER JOIN permissions AS `GroupPermission` ON (`GroupPermission`.`model` = 'Group' AND `GroupPermission`.`foreignId` = `Group`.`id` AND (...))
WHERE `Tag`.`keyname` = 'europe'
如何在belongsTo加入后在beforeFind()中添加我的联接?
答案 0 :(得分:0)
连接不能引用关联中的任何内容,因为关联是通过单独的查询获取的。如果您需要添加引用关联的联接,则还需要在联接中手动添加关联。