如何在ZF2中使用CONCAT加入sql请求

时间:2012-10-23 13:39:28

标签: sql zend-framework2

我正在将我的zf1应用程序移动到zf2,我在移动我的映射器时遇到了很多问题。 显然,我不能只是复制/过去我的查询(太糟糕了!)

我正在尝试在

等联接中使用CONCAT
$resultSet = $this->select(function (Select $select) use($userId, $filter, $status){ 

 $select->columns(array("id_project", "name", "current_to", "avancement"));
 $prj = "PRJ_";
 $select->join(array("ru" => "role_users"), "CONCAT( $prj , projects.id_project ) = ru.element", array())
                   ->where("ru.user_id = $userId");

如果我打印$select->getSqlString(),我会:

    SELECT "projects"."id_project" AS "id_project", "projects"."name" AS "name", 
    "projects"."current_to" AS "current_to", "projects"."avancement" AS "avancement" FROM 
"projects" INNER JOIN "role_users" AS "ru" ON "CONCAT"( "PRJ_" "," "projects"."id_project"
 ) = "ru"."element" WHERE ru.user_id = 3

Apport for all“”,这是正确的(如果我删除所有“”并在phpmyadmin中执行它,它可以工作。 但是,如果zf2正在执行它,我会收到一个错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in 
your SQL syntax; check the manual that corresponds to your MySQL server version 
for the right syntax to use near '`projects`.`id_project` ) = `ru`.`element` 
WHERE ru.user_id = 3' at line 1

看起来CONCAT()存在问题,但我无法找到问题所在。

1 个答案:

答案 0 :(得分:0)

我觉得这不是很优雅但是有效:

$select->join(array("ru" => "role_users"), 
new Expression("CONCAT( $prj , projects.id_project ) = ru.element"), array())
->where("ru.user_id = $userId"));