1

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

Einius
  • 1,352
  • 2
  • 20
  • 45

3 Answers3

1

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.

Damaged Organic
  • 8,175
  • 6
  • 58
  • 84
1

It's move clear to keep this things out of controller - more readable.

->add('approvers', 'entity', array(
    'class' => 'YourBundle:Entity',
    'query_builder' => function(EntityRepository $er) {
        return $er->orderBy('name', 'ASC'); // Here you can make some custome query
     },
     'label' => 'label',
))
jamek
  • 792
  • 10
  • 20
0

Please check this:

->add('usr_role', EntityType::class, array('label' => 'Role : ',
     'class' => \Ibw\UserBundle\Entity\UserRole::class,
     'expanded' => false, 'placeholder' => 'Select Role',
     'multiple' => false))
Ashwin
  • 450
  • 1
  • 6
  • 17