bre*_*req 13 php pdo symfony doctrine-orm
我的SQL看起来像这样:
$sql = "select * from user where id in (:userId) and status = :status";
$em = $this->getEntityManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->bindValue(':userId', $accounts, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
$stmt->bindValue(':status', 'declined');
$stmt->execute();
$result = $stmt->fetchAll();
但它返回:
执行(...)时发生异常
参数[[1,2,3,4,5,6,7,8,11,12,13,14],"谢绝"]
注意:数组到字符串转换
我无法使用,queryBuilder因为我的真实SQL更复杂(例如包含连接的选择,联合等)
Cer*_*rad 16
您不能将预准备语句与数组一起使用,因为sql本身不支持数组.这是一个真正的耻辱.沿着这条线的某个地方,您实际需要确定您的数据是否包含三个项并发出一个IN(?,?,?).Doctrine ORM实体管理器会自动为您完成此操作.
幸运的是,DBAL让你满意.你只是不使用绑定或准备.该手册有一个例子:http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion
在你的情况下,它看起来像:
$sql = "select * from user where id in (?) and status = ?";
$values = [$accounts,'declined'];
$types = [Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR];
$stmt = $conn->executeQuery($sql,$values,$types);
$result = $stmt->fetchAll();
上面的代码未经测试,但您应该明白这一点.(确保你use Doctrine\DBAL\Connection;的Connection::PARAM_INT_ARRAY)
使用命名参数的人的注意事项:
如果使用命名参数(:param而不是?),则在提供类型时应尊重参数名称.例如:
$sql = "select * from user where id in (:accounts) and status = :status";
$values = ['accounts' => $accounts, 'status' => 'declined'];
$types = ['accounts' => Connection::PARAM_INT_ARRAY, 'status' => \PDO::PARAM_STR];
| 归档时间: | 
 | 
| 查看次数: | 16350 次 | 
| 最近记录: |