如何在 CakePHP 3.0 中复制 Query::notMatching()?
How to replicate Query::notMatching() in CakePHP 3.0?
我不喜欢使用 cakephp 3.1 beta。我正在使用 3.0 版。我正在尝试查找所有没有特定标签的帖子。我该怎么做?
为了复制 notMatching()
的行为,您必须添加一个适当的 LEFT
加入条件以匹配应排除的标签,另一个 LEFT
加入包括连接 table,然后在主查询上使用条件,通过 IS NULL
检查连接 table 的主键来排除具有匹配标签的行。
这是一个基本示例,假设 Posts belongsToMany Tags
关系
$query = $Posts
->find()
->leftJoin(
['Tags' => 'tags'],
['Tags.title' => 'specificTagToExlcude']
)
->leftJoin(
['PostsTags' => 'posts_tags'],
'Posts.id = PostsTags.post_id AND Tags.id = PostsTags.tag_id'
)
->where([
'PostsTags.id IS NULL'
]);
这将创建一个类似于
的查询
SELECT
Posts.id AS `Posts__id`,
// ...
FROM
posts Posts
LEFT JOIN
tags Tags
ON Tags.title = 'specificTagToExlcude'
LEFT JOIN
posts_tags PostsTags
ON PostsTags.post_id = Posts.id
AND PostsTags.tag_id = Tags.id
WHERE
PostsTags.id IS NULL
我不喜欢使用 cakephp 3.1 beta。我正在使用 3.0 版。我正在尝试查找所有没有特定标签的帖子。我该怎么做?
为了复制 notMatching()
的行为,您必须添加一个适当的 LEFT
加入条件以匹配应排除的标签,另一个 LEFT
加入包括连接 table,然后在主查询上使用条件,通过 IS NULL
检查连接 table 的主键来排除具有匹配标签的行。
这是一个基本示例,假设 Posts belongsToMany Tags
关系
$query = $Posts
->find()
->leftJoin(
['Tags' => 'tags'],
['Tags.title' => 'specificTagToExlcude']
)
->leftJoin(
['PostsTags' => 'posts_tags'],
'Posts.id = PostsTags.post_id AND Tags.id = PostsTags.tag_id'
)
->where([
'PostsTags.id IS NULL'
]);
这将创建一个类似于
的查询SELECT
Posts.id AS `Posts__id`,
// ...
FROM
posts Posts
LEFT JOIN
tags Tags
ON Tags.title = 'specificTagToExlcude'
LEFT JOIN
posts_tags PostsTags
ON PostsTags.post_id = Posts.id
AND PostsTags.tag_id = Tags.id
WHERE
PostsTags.id IS NULL