带有数组值的Doctrine DBAL setParameter()

时间:2015-07-06 10:06:01

标签: php mysql symfony doctrine dbal

我使用了doctrine DBAL,并且由于queryBuilder而导致SQL查询出现问题。

$builder = $this->getConnection()->getQueryBuilder();
$builder->select(['id','name','type'])
         ->from('table')
         ->where('id='.(int)$value)
         ->setMaxResults(1);
$builder->andWhere($builder->expr()->in('type', ['first','second']));

echo(builder->getSQL());

$data = $builder->execute()->fetchRow();

获取SQL

SELECT id, name, type FROM table WHERE (id=149) AND (type IN (first,second)) LIMIT 1

这就是问题,我需要(输入IN(第一,第二))编码为字符串(类型IN(&#39; first&#39;,&#39; second&#39;))< / p>

如何以正确的方式使用查询构建器执行此操作?

1 个答案:

答案 0 :(得分:23)

尝试

$builder->andWhere('type IN (:string)');
$builder->setParameter('string', array('first','second'), \Doctrine\DBAL\Connection::PARAM_STR_ARRAY);