Sql Doctrine2:带参数的DQL

Sql Doctrine2:带参数的DQL,sql,doctrine-orm,sql-order-by,dql,Sql,Doctrine Orm,Sql Order By,Dql,我想把排序类型作为参数。所以我写了一个函数 public function findInterval($pageNumber, $limit, $sortType) { $query = $this->_em->createQuery('Select c from Entities\Comment c where c.isremoved=0 ORDER BY c.creationdate ?1'); $query->setParameter(1, $sortTy

我想把排序类型作为参数。所以我写了一个函数

public function findInterval($pageNumber, $limit, $sortType) {
    $query = $this->_em->createQuery('Select c from Entities\Comment c where c.isremoved=0 ORDER BY c.creationdate ?1');
    $query->setParameter(1, $sortType);  //sortType is either ASC or DESC

    return $users = $query->getResult();
}
但它不适用于致命错误 未捕获异常“条令\ORM\Query\QueryException”,消息为“[Syntax Error]第0行,第77列:错误:字符串的预期结尾,got”?在C:\Users\user\Desktop\projects\interview\application\libraries\doctor\ORM\Query\QueryException.php:42堆栈跟踪:0 C:\Users\user\Desktop\projects\interview\application\libraries\doctor\ORM\Query\Parser.php380:doctor\ORM\Query\QueryException::syntaxError'行0中,col 77:…'1 C:\Users\user\Desktop\projects\interview\application\libraries\doctor\ORM\Query\Parser.php745:doctor\ORM\Query\Parser->syntaxError'end of string'2 C:\Users\user\Desktop\projects\interview\application\libraries\doctor\ORM\Query\Parser.php213:doctor\ORM\Query\Parser\Parser->查询语言3C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\Query\Parser.php288:Doctrine\ORM\Query\Parser->getAST 4 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\Query\Parser->parse 5 C:\Users\user\Deskt in第42行的C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\Query\QueryException.php


有没有其他方法可以按参数设置排序类型?

您只能绑定在where in prepared语句中使用的参数。无论如何都不需要在orderBy中使用它,因为在该部分上不可能进行SQL注入

只需使用普通PHP:

$sortType = ($sortType == 1) ? 'ASC' : 'DESC';
$query = $this->_em->createQuery('Select c from Entities\Comment c where c.isremoved=0 ORDER BY c.creationdate ' . $sortType);

您只能绑定在where-in-prepared语句中使用的参数。无论如何都不需要在orderBy中使用它,因为在该部分上不可能进行SQL注入

只需使用普通PHP:

$sortType = ($sortType == 1) ? 'ASC' : 'DESC';
$query = $this->_em->createQuery('Select c from Entities\Comment c where c.isremoved=0 ORDER BY c.creationdate ' . $sortType);

首先,将值直接放入DQL c.isremoved=0,正如Bram正确指出的那样,这不应该发生。您应该只将参数绑定到查询,这些参数将被正确转义并减轻任何潜在的SQL注入攻击

其次,您使用的$sortType参数应该包含ASC或DESC。不确定您希望传递给此函数的值。但正如Bram所演示的,应该对其进行测试,以确保您只使用这两个值中的一个

public function findInterval($pageNumber, $limit, $sortType) 
{
    $sortType = ($sortType == 'ASC') ? $sortType : 'DESC';    // <-- this example defaults to descending
    $query = $this->_em->createQuery('SELECT c FROM Entities\Comment c WHERE c.isremoved = :isremoved ORDER BY c.creationdate ' . $sortType);
    $query->setParameter('isremoved', 0);

    return $users = $query->getResult();
}

首先,将值直接放入DQL c.isremoved=0,正如Bram正确指出的那样,这不应该发生。您应该只将参数绑定到查询,这些参数将被正确转义并减轻任何潜在的SQL注入攻击

其次,您使用的$sortType参数应该包含ASC或DESC。不确定您希望传递给此函数的值。但正如Bram所演示的,应该对其进行测试,以确保您只使用这两个值中的一个

public function findInterval($pageNumber, $limit, $sortType) 
{
    $sortType = ($sortType == 'ASC') ? $sortType : 'DESC';    // <-- this example defaults to descending
    $query = $this->_em->createQuery('SELECT c FROM Entities\Comment c WHERE c.isremoved = :isremoved ORDER BY c.creationdate ' . $sortType);
    $query->setParameter('isremoved', 0);

    return $users = $query->getResult();
}