CakePHP: difference between backtick query and normal query
<p>I have 3 blog tables in my CakePHP application that are linked with a HABTM association following the CakePHP naming conventions: posts - post_tag_links - tags.</p>

<p>I try to get an array with all the posts that are linked to a specific tag (e.g. "design"). This query works for me:</p>

<pre><code>$this-&gt;Post-&gt;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"
);
</code></pre>

<p>CakePHP then generates the following query and gave me 4 results:</p>

<pre><code>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
</code></pre>

<p>BUT... to do some best practice, it's better to not use the "query" method. So I tried the "find all" method:</p>

<pre><code>$this-&gt;Post-&gt;find('all', array(
    'fields' =&gt; array(
        'Post.id',
        'Post.title'
    ),
    'joins' =&gt; array(
        array(
            'table' =&gt; 'post_tag_links',
            'alias' =&gt; 'PostTagLink',
            'type' =&gt; 'LEFT',
            'conditions' =&gt; array(
                'Post.id' =&gt; 'PostTagLink.post_id'
            )
        ),
        array(
            'table' =&gt; 'tags',
            'alias' =&gt; 'Tag',
            'type' =&gt; 'LEFT',
            'conditions' =&gt; array(
                'Tag.id' =&gt; 'PostTagLink.tag_id',
            )
        )
    ),
    'conditions' =&gt; array(
        'Tag.slug' =&gt; 'design'
    ),
    'group' =&gt; 'Post.id'
    )
));
</code></pre>

<p>CakePHP then generates the following query and gave NO single result:</p>

<pre><code>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
</code></pre>

<p>After a lot of trial and error, I discovered the problem is the backticks that CakePHP creates when building up that last query.</p>

<p>My question is: what's the difference between the query with the backticks and the one without the backticks? And how can you leave those backticks in CakePHP?</p>

<p>Thanks ;)</p>