
[英]FOSFacebookBunlde throws “Notice: Array to string conversion in vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 103” exception
[英]Set parameter as int array for where in statement in dbal query builder throws array to string conversion exception
我尝试在WHERE IN语句中的dbal querybuilder中绑定参数:[1,2]
我试图将$qb->expr()->in()
改为字符串版本,但没有改变
QueryBuilder创建
$qb = $this->_em->getConnection()->createQueryBuilder()
->select('c.id AS id')
->from('category', 'c')
->andWhere($qb->expr()->in('c.id', ':categories'))->setParameter('categories', [1, 2], \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
执行:
$qb->execute()->fetchAll();
错误: Array to string conversion
期望将整数数组绑定到querybuilder语句
总是喜欢引用文档。
// Example - $qb->expr()->in('u.id', array(1, 2, 3))
// Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception.
// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above)
public function in($x, $y); // Returns Expr\Func instance
来源: doctrine querybuilder docs
基本上,除非类别是数字,否则你必须用占位符填充数组并设置它们,或者如果它们是数字,你可能只是使用这个例子。
我假设你有一个实体,它有一个带有Category实体的ManyToMany或ManyToOne,你可以只传递一个Categories数组,如果数组是这样正确制作的,它应该可以工作:
qb = $this->_em->getConnection()->createQueryBuilder()
->select('c.id AS id')
->from('category', 'c')
->andWhere($qb->expr()->in('c.id',':categories'))->setParameter('categories', $categoriesArray);
否则,您可以尝试通过插入数组来将setParameter与您的id数组一起使用:
qb = $this->_em->getConnection()->createQueryBuilder()
->select('c.id AS id')
->from('category', 'c')
->andWhere($qb->expr()->in('c.id', ':categories'))->setParameter('categories', implode(",",[1, 2]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.