P
P
Pavel2014-07-21 15:11:30
symfony
Pavel, 2014-07-21 15:11:30

Creating and editing a form with choice fields in Symfony?

I torment Symfony, and she me. She's doing better.) I create forms and collections of the same type without problems, but I ran into forms based on related data. Couldn't find an answer. Either I'm stupid, or everyone is smarter). I am making a FAQ section on the site, which contains a list of sections (chapters) and questions with answers for each chapter.
The essence of the problem.
There are two Doctrine entities - sections (Item) and questions and answers (Faq) for them. Accordingly, with a one-to-many relationship (each section has several questions).
I can’t figure out how to create a form to enter a new question into it and indicate by selecting which section (chapter) it belongs to from the drop-down list.
Is there an out-of-the-box way to display existing sections, or do I need to create two forms in one? In one, pre-select and generate data for the tag (in a separate built-in form) from the section entity (Item) and the second with empty fields for the question and answer? Somehow it is not beautiful and cumbersome .. And then how to edit the old data?
This is what is needed all the time. Maybe there is a ready-made example of this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Solovyov, 2014-07-21
@tdstdk

if I understood correctly, then you need to create the item_id field in the faq form, with the entity type and select all items in the query_builder parameter and that's it.
Here are the options

->add('item', 'entity', array(
    'class' => 'AcmeHelloBundle:Item',
    'property' => 'title',
));

$builder->add('item', 'entity', array(
    'class' => 'AcmeHelloBundle:Item',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('u')
            ->orderBy('u.username', 'ASC');
    },
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question