doctrine 2 querybuilder with set parameters not working

感情迁移 提交于 2019-12-02 02:49:56

You can't use placeholders for dinamical build of DQL query. You'll have to code it by your own:

$sortBy = in_array($sortBy, array(...)) ? $sortBy : 'id';
$sortDir = $sortDir == 'ASC' ? 'ASC' : 'DESC';

$this->em->createQueryBuilder()
    ...
    ->orderBy('u.' . $sortBy, $sortDir)

You cant bind parameters to QueryBuilder, only to Query, so just swap lines, first get query out of builder, then fill it with parameters and get result.

$query = $this->_em->createQueryBuilder()
            ->select('u')
            ->from('\Entities\Users', 'u')
            ->where('u.userid= ?1')
            ->orderBy('u.?3', '?3')
            ->orderBy('u.'.$orderBy, $sort)
            ->getQuery()
            ->setParameter(1, $userid)
            ->getResult();

}

In doctrine 2.4 its fixed, and you can bind parameters to QueryBuilder.

Update: i've missed moment with placeholder in field name, SQL do not support such constructions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!