我正在尝试在 DQL 中使用 Doctrine2 对 mysql 数据库执行更新查询。这是我的代码:
$updateFiles = $entityManager
->createQuery("UPDATE Attachment a "
. "SET a.filename=CONCAT(:dst, TRIM(LEADING :src FROM a.filename)) "
. "WHERE a.owner=:ownerId AND a.filename LIKE :srcLike");
$updateFiles->setParameter("dst", $dstFile);
$updateFiles->setParameter("src", $srcFile);
$updateFiles->setParameter("ownerId", $this->owner->getId());
$updateFiles->setParameter("srcLike", $srcFile.'/%');
$updateFiles->execute();
当我执行此代码时,出现以下语法错误:
[Syntax Error] line 0, col 77: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got ':src'
我尝试在不使用 TRIM 和 CONCAT 的情况下执行查询并且它有效。如果我将 :src 替换为“somerandomtext”,则没有错误。 TRIM 的语法是 correct !由于某些原因,参数 :src 没有被替换。
这是一个错误还是我错过了什么?
最佳答案
我查看了 DQL 中 TRIM 函数的实现代码,似乎我不能为字符“trchar”使用参数。
TRIM([LEADING | TRAILING | BOTH] [‘trchar’ FROM] str)
所以我将把字符串与查询连接起来:
$updateFiles = $entityManager
->createQuery("UPDATE Attachment a "
. "SET a.filename=CONCAT(:dst, TRIM(LEADING $srcFile FROM a.filename)) "
. "WHERE a.owner=:ownerId AND a.filename LIKE :srcLike");
关于php - 带参数的 Doctrine2 DQL TRIM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33683330/