Symfony 使用数组参数创建查询
我尝试执行这个查询Symfony 使用数组参数创建查询,symfony,doctrine-odm,Symfony,Doctrine Odm,我尝试执行这个查询 $em->createQuery('select i from AcmePublicBundle:Public i where i.user in (:opc)'); $query->setParameter('opc', $opc); where :opc is an array like this $opc={array}[2] 0 = {array}[1] id = "9" 1 = {array}[1] id = "10"
$em->createQuery('select i from AcmePublicBundle:Public i where i.user in (:opc)');
$query->setParameter('opc', $opc);
where :opc is an array like this
$opc={array}[2]
0 = {array}[1]
id = "9"
1 = {array}[1]
id = "10"
我得到这个错误
Notice: Undefined offset: 0 in...
如果我试试这个
$em->createQuery('select i from AcmePublicBundle:Public i where i.usuario in (9, 10)');
一切都很好。但我必须通过$opc
有什么想法吗?是的,这个错误看起来很合适,因为
setParameter
同时考虑了array
键和值,键代表字段名。因此,这里不能使用矩阵
$ids = array();
foreach ( $opc as $o ){
$ids[] = (int)$o[1]; //be careful about data type (string, int etc)
}
$q = " .... WHERE id IN (:opc) ... ";
$q->setParameter('opc', $ids);
这应该可以工作….您的
$opc
数组不应该是多维的。尝试像这样传递数组:
$opc=array(
'9',
'10'
);
而不是
$opc=array(
array('id'=>'9'),
array('id'=>'10')
);