I have two entities: Event and City. And I want to implement create event form. but one of the fields should be dropdown list with the values from database (City entity).
currently I have that in my controller:
$city = $this->getDoctrine()
->getRepository('AtotrukisMainBundle:City')
->findBy(
array(),
array('priority' => 'ASC', 'name' => 'ASC')
);
$event = new Event();
$form = $this->createFormBuilder($event)
->add('name', 'text')
->add('description', 'textarea')
->add('startDate', 'datetime')
->add('endDate', 'datetime')
->add('map', 'text')
->add('city', 'choice', array(
'choice_list' => new ChoiceList($city->getId(), $city->getName())
))
->add('save', 'submit', array('label' => 'Sukurti'))
->getForm();
$form->handleRequest($request);
But with that I get error:
Error: Call to a member function getId() on array in /var/www/src/Atotrukis/MainBundle/Controller/EventController.php line 31
Anyway, solution could be:
foreach($city as $value) {
$id_set[] = $value->getId();
$name_set[] = $value->getName();
}
//...
->add('city', 'choice', array(
'choice_list' => new ChoiceList($id_set, $name_set)
))
Because ChoiceList expects an arrays as arguments. You are trying to use methods on array.