1

I try to execute this query

$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" 

I get this error

Notice: Undefined offset: 0 in...

If I try this

$em->createQuery('select i from AcmePublicBundle:Public i where i.usuario in (9, 10)');

everything is fine. But I have to pass $opc.

Any ideas?

2 Answers 2

2

Yeah, error seems quite appropriate because setParameter takes both array key and value into account and key represents field name. Therefore, you can't use matrix here.

$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);

This should work....

Sign up to request clarification or add additional context in comments.

Comments

1

Your $opc array should not be multidimensional. Try passing an array like this:

$opc=array(
  '9',
  '10'
);

instead of

$opc=array(
  array('id'=>'9'),
  array('id'=>'10')
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.