如何在DQL中使用RETURNING更新查询

时间:2019-04-05 03:53:38

标签: php doctrine-orm zend-framework2 dql

我正在尝试通过以下简单查询运行这些命令(在PostgreSQL中有效)

update ja_clients set ref_inc_num = ref_inc_num + 1  where id = 43933 returning ref_inc_num;

进入DQL

$query = $this->entityManager->createQuery();
            $query->setDQL("UPDATE Geoop\Core\Entity\Account a SET a.jobReferenceNumber__ = a.jobReferenceNumber__ + 1  WHERE a.id = :accountId  RETURNING a.jobReferenceNumber__");
            $query->setParameter('accountId', $account->getId());
            $total = $query->getSingleScalarResult();

但重新调整此错误无效:

#30 /var/www/geoop_api/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php (line 861): Doctrine\ORM\Query\Parser->syntaxError(end of string)
#31 /var/www/geoop_api/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php (line 448): Doctrine\ORM\Query\QueryException::dqlError(UPDATE Geoop\Core\Entity\Account a SET a.jobReferenceNumber__ = a.jobReferenceNumber__ + 1  WHERE a.id = :accountId  RETURNING a.id as id)

2 个答案:

答案 0 :(得分:1)

DQL不支持 document.getElementById("calc").onclick = function calculate(){ var rate = +document.getElementById("rate").value/100; var years = +document.getElementById("years").value; var principal = +document.getElementById("principal").value; var i = 0; while(i < years){ var afterRate = principal * rate; var totalAmount = principal += afterRate; var interestValue = document.createElement("div"); var division = document.getElementById('divis'); interestValue.style.color = "white"; interestValue.style.fontFamily = "futura"; interestValue.innerHTML = totalAmount; document.division.appendChild(interestValue); var lastButton = document.getElementById("last"); var clearButton = document.getElementById("clear"); var roundButton = document.getElementById("round"); i++ } } 子句,因此您需要use native SQL。您还必须create a result set mapping

注意:我不知道您的表和列的实际名称,但是假设它们的名称与您的实体和属性相同,那么它将起作用:

RETURNING

在我的示例中,您可能需要更正表名和列名。使用本机SQL时,必须使用数据库中的实际名称,而不是实体和属性的名称。另外,请记住,postgres会将所有对象名都转换为大写,除非用引号引起来。

答案 1 :(得分:0)

感谢您的见解:)现在可以正常使用

这里是我的更新代码: 在课程上方,我使用ResultSetMapping

use Doctrine\ORM\Query\ResultSetMapping;

$rsm = new ResultSetMapping();
$rsm->addScalarResult('ref_inc_num', 'ref_inc_num');
$sql = "
UPDATE ja_clients
SET ref_inc_num = ref_inc_num + 1
WHERE id = :clientId
RETURNING ref_inc_num
";
$query = $this->entityManager->createNativeQuery($sql, $rsm);
$query->setParameter('clientId', $account->getId());
$refNum = $query->getSingleScalarResult();