CakePHP:反引号查询和普通查询的区别
CakePHP: difference between backtick query and normal query
我的 CakePHP 应用程序中有 3 个博客表,这些表与遵循 CakePHP 命名约定的 HABTM 协会链接:帖子 - post_tag_links - 标签。
我尝试获取一个数组,其中包含链接到特定标签的所有帖子(例如 "design")。这个查询对我有用:
$this->Post->query("
SELECT
Post.id, Post.title FROM posts AS Post
LEFT JOIN
post_tag_links AS PostTagLink ON Post.id = PostTagLink.post_id
LEFT JOIN
tags AS Tag ON Tag.id = PostTagLink.tag_id
WHERE
Tag.slug = 'design'
GROUP BY
Post.id"
);
CakePHP 然后生成以下查询并给了我 4 个结果:
SELECT
Post.id,
Post.title
FROM
posts AS Post
LEFT JOIN
post_tag_links AS PostTagLink
ON Post.id = PostTagLink.post_id
LEFT JOIN
tags AS Tag
ON Tag.id = PostTagLink.tag_id
WHERE
Tag.slug = 'design'
GROUP BY
Post.id
但是...为了做一些最佳实践,最好不要使用 "query" 方法。所以我尝试了 "find all" 方法:
$this->Post->find('all', array(
'fields' => array(
'Post.id',
'Post.title'
),
'joins' => array(
array(
'table' => 'post_tag_links',
'alias' => 'PostTagLink',
'type' => 'LEFT',
'conditions' => array(
'Post.id' => 'PostTagLink.post_id'
)
),
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'LEFT',
'conditions' => array(
'Tag.id' => 'PostTagLink.tag_id',
)
)
),
'conditions' => array(
'Tag.slug' => 'design'
),
'group' => 'Post.id'
)
));
CakePHP 然后生成以下查询并没有给出单一结果:
SELECT
`Post`.`id`,
`Post`.`title`
FROM
`kattenbelletjes`.`posts` AS `Post`
LEFT JOIN
`kattenbelletjes`.`post_tag_links` AS `PostTagLink`
ON (
`Post`.`id` = 'PostTagLink.post_id'
)
LEFT JOIN
`kattenbelletjes`.`tags` AS `Tag`
ON (
`Tag`.`id` = 'PostTagLink.tag_id'
)
WHERE
`Tag`.`slug` = 'design'
GROUP BY
`Post`.`id
经过反复试验,我发现问题出在 CakePHP 在构建最后一个查询时创建的反引号。
我的问题是:带反引号的查询和不带反引号的查询有什么区别?以及如何在 CakePHP 中保留这些反引号?
谢谢 ;)
反引号很可能不是问题,因为它们所做的只是转义标识符。顺便说一句,这是一个很容易找到的东西。
- http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
- Using backticks around field names
实际问题更可能是您以错误的方式定义了条件,您在那里所做的是创建字符串文字比较条件,即
`Post`.`id` = 'PostTagLink.post_id'
将 id
列值与字符串 PostTagLink.post_id
进行比较当然会失败。
定义标识符比较的正确方法是将条件片段作为单个值而不是键 => 值集提供,即
'conditions' => array(
'Post.id = PostTagLink.post_id'
)
和
'conditions' => array(
'Tag.id = PostTagLink.tag_id'
)
另见
我的 CakePHP 应用程序中有 3 个博客表,这些表与遵循 CakePHP 命名约定的 HABTM 协会链接:帖子 - post_tag_links - 标签。
我尝试获取一个数组,其中包含链接到特定标签的所有帖子(例如 "design")。这个查询对我有用:
$this->Post->query("
SELECT
Post.id, Post.title FROM posts AS Post
LEFT JOIN
post_tag_links AS PostTagLink ON Post.id = PostTagLink.post_id
LEFT JOIN
tags AS Tag ON Tag.id = PostTagLink.tag_id
WHERE
Tag.slug = 'design'
GROUP BY
Post.id"
);
CakePHP 然后生成以下查询并给了我 4 个结果:
SELECT
Post.id,
Post.title
FROM
posts AS Post
LEFT JOIN
post_tag_links AS PostTagLink
ON Post.id = PostTagLink.post_id
LEFT JOIN
tags AS Tag
ON Tag.id = PostTagLink.tag_id
WHERE
Tag.slug = 'design'
GROUP BY
Post.id
但是...为了做一些最佳实践,最好不要使用 "query" 方法。所以我尝试了 "find all" 方法:
$this->Post->find('all', array(
'fields' => array(
'Post.id',
'Post.title'
),
'joins' => array(
array(
'table' => 'post_tag_links',
'alias' => 'PostTagLink',
'type' => 'LEFT',
'conditions' => array(
'Post.id' => 'PostTagLink.post_id'
)
),
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'LEFT',
'conditions' => array(
'Tag.id' => 'PostTagLink.tag_id',
)
)
),
'conditions' => array(
'Tag.slug' => 'design'
),
'group' => 'Post.id'
)
));
CakePHP 然后生成以下查询并没有给出单一结果:
SELECT
`Post`.`id`,
`Post`.`title`
FROM
`kattenbelletjes`.`posts` AS `Post`
LEFT JOIN
`kattenbelletjes`.`post_tag_links` AS `PostTagLink`
ON (
`Post`.`id` = 'PostTagLink.post_id'
)
LEFT JOIN
`kattenbelletjes`.`tags` AS `Tag`
ON (
`Tag`.`id` = 'PostTagLink.tag_id'
)
WHERE
`Tag`.`slug` = 'design'
GROUP BY
`Post`.`id
经过反复试验,我发现问题出在 CakePHP 在构建最后一个查询时创建的反引号。
我的问题是:带反引号的查询和不带反引号的查询有什么区别?以及如何在 CakePHP 中保留这些反引号?
谢谢 ;)
反引号很可能不是问题,因为它们所做的只是转义标识符。顺便说一句,这是一个很容易找到的东西。
- http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
- Using backticks around field names
实际问题更可能是您以错误的方式定义了条件,您在那里所做的是创建字符串文字比较条件,即
`Post`.`id` = 'PostTagLink.post_id'
将 id
列值与字符串 PostTagLink.post_id
进行比较当然会失败。
定义标识符比较的正确方法是将条件片段作为单个值而不是键 => 值集提供,即
'conditions' => array(
'Post.id = PostTagLink.post_id'
)
和
'conditions' => array(
'Tag.id = PostTagLink.tag_id'
)
另见