You need to use the use option for the closure as the variable your trying to use isn't in the correct scope.
See example 3 on this page: http://php.net/manual/en/functions.anonymous.php Your code will look like this: 'query_builder' => function($repository) use ($state_id) { return $repository->createQueryBuilder('cities')- >where('cities.state_id = :state_id')->setParameter('state_id', $state_id); } On Feb 23, 1:19 am, Roger Webb <webb.ro...@gmail.com> wrote: > Another interesting point to add. If I define(STATE_ID, $state_id) > and pass that to the QueryBuilder setParameter method it works. > > On Feb 21, 3:12 pm, Roger Webb <webb.ro...@gmail.com> wrote: > > > > > > > > > Greetings, > > > Ran into an issue with the EntityChoiceField. I am rendering a SELECT > > field with city names. If I use a literal value (ie. 2) in the > > query_builder param of the EntityChoiceField everything works > > perfectly. If I substitute that with a variable value I get an empty > > select. Also, I have printed out the value of the state_id from the > > request and it prints the correct value. > > > Any help would be appreciated. > > > Roger > > > Here is the code where I create the field. > > > This is the code for the controller that is fetching the select field > > > $params['city_select'] = > > $this->forward('UserBundle:Ajax:getCitySelectByStateId', array(), > > > array('state_id' => 2))->getContent(); > > > $params gets passed to the view > > > This is the code from my AjaxController which should return these db > > driven fields on demand. > > > public function getCitySelectByStateIdAction() { > > $em = $this->get('doctrine.orm.entity_manager'); > > $request = $this->get('request'); > > > if(!$request->get('state_id')) > > return new Response('ERROR'); //NOT GETTING AN ERROR > > HERE > > > $state_id = (int)$request->get('state_id'); > > > $field = new EntityChoiceField($name, array( > > 'em' => $em, > > 'class' => 'ARN\EntityBundle\Entity\Cities', > > 'property' => 'city_name', > > 'query_builder' => function($repository) { > > return > > $repository->createQueryBuilder('cities')->where('cities.state_id = > > :state_id')->setParameter('state_id', > > > $state_id); > > } > > )); > > > return $this->render('UserBundle:Ajax:form_field.html.twig', > > array('field' => $field)); > > } -- If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com You received this message because you are subscribed to the Google Groups "symfony users" group. To post to this group, send email to symfony-users@googlegroups.com To unsubscribe from this group, send email to symfony-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/symfony-users?hl=en