We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Query builder leftJoin error

Hi. I use query builder to make some filters system and i found this bug:

$cUser = $this->currentUser;
if ($cUser) {
    $builder->leftJoin("StoredSkins", "st.skin_id = s.id AND st.user_id = $cUser->id", "st");
    $builder->columns($builder->getColumns().", CASE WHEN st.id IS NULL THEN 0 ELSE 1 END AS stored");
}

On output i have this query:

SELECT s.id, s.hash, s.count_comments, s.count_savers, s.count_cubes, s.count_views_total, s.color_red, s.color_green, s.color_blue, CASE WHEN st.id IS NULL THEN 0 ELSE 1 END AS stored FROM skins AS s LEFT JOIN stored_skins AS st ON st.skin_id = s.id AND st.user_id = 15 ORDER BY s.count_views_per_week DESC, s.count_views_total DESC LIMIT 40

And it's greatly works, if i paste this in my phpMyAdmin, but Phalcon say:

Syntax error, unexpected token IDENTIFIER(st), near to '.id IS NULL THEN 0 ELSE 1 END AS stored FROM [Skins] AS [s] LEFT JOIN [StoredSkins] AS [st] ON st.skin_id = s.id AND st.user_id = 15 ORDER BY s.count_views_per_week DESC, s.count_views_total DESC LIMIT 40', when parsing: SELECT s.id, s.hash, s.count_comments, s.count_savers, s.count_cubes, s.count_views_total, s.color_red, s.color_green, s.color_blue, CASE WHEN st.id IS NULL THEN 0 ELSE 1 END AS stored FROM [Skins] AS [s] LEFT JOIN [StoredSkins] AS [st] ON st.skin_id = s.id AND st.user_id = 15 ORDER BY s.count_views_per_week DESC, s.count_views_total DESC LIMIT 40 (349)

How can i fix this?

Wow, quick response and the correct decision. Thank you very much!