Doctrine DBAL setParameter() with array value

2022-08-31 00:16:09

我正在使用原则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('first','second'))

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


答案 1

尝试

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

答案 2
$builder
    ->andWhere($builder->expr()->in('type', ':types'))
    ->setParameter(':types', ['first','second'], \Doctrine\DBAL\Connection::PARAM_STR_ARRAY);

推荐