gpt4 book ai didi

php - 具有数组值的 Doctrine DBAL setParameter()

转载 作者:IT王子 更新时间:2023-10-29 00:30:55 26 4
gpt4 key购买 nike

我正在使用 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

这就是问题所在,我需要将 (type IN (first,second)) 编码为字符串,例如 (type IN ('first','second'))

如何以正确的方式使用查询生成器做到这一点?

最佳答案

试试

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

关于php - 具有数组值的 Doctrine DBAL setParameter(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31243022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com