E
E
Elena2015-04-27 15:21:23
symfony
Elena, 2015-04-27 15:21:23

How to keep the grouping when displaying a collection in a form?

In particular, there is a specialist whose services are indicated.

Specialist {#364 ▼
  -id: 1
  -name: "Иванов Иван Иванович"
  -phone: "8887779999"
  -address: Address {#421 ▶}
  -services: array:2 [▼
    "Профилактические процедуры" => array:2 [▼
      0 => Service {#392 ▶}
      1 => Service {#446 ▶}
    ]
    "Хиругрические процедуры" => array:1 [▼
      0 => Service {#432 ▶}
    ]
  ]
  -reservations: PersistentCollection {#376 ▶}
  -unsorted_services: PersistentCollection {#402 ▶}
}

Services are categorized here. But when after createView this division disappears. And in the template, the services are "turned" into a one-dimensional array from the FormView.
How to make such division in the form? Perhaps you need to do something with buildView or finishView, but the documentation could not be found.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-04-27
@lenk0belk0

Here is the documentation
For collections
Splitting only works on...
... 'entity', array(
'expanded' => false,
'multiple' => true,
)
Symfony uses optgroup for the view (flags fly by)
https://github. com/symfony/symfony/issues/1735
The only thing that immediately catches your eye are the keys... They are supposed to be a field in the Service class. (in formBuilder'e group_by add this field and everything will be fine).
If the keys cannot be taken out, then transform this collection into a choice_list. And there at least triple nesting in select.
===============================================
UPD: Without transformation, you will have to assemble the entire form with pens, this is a rather dull task and a lot of code comes out for checking, validating, setting selected fields (editing), only because of the grouping into categories.
Each array that in the services Specialist field should be made into a wrapper class. It has 2 fields - name, services and with methods addService, removeService, getServices, getName, setName. Setting data in the form, through the events FormEvents::PRE_SET_DATA.
This will be the best option if there is no way to change the structure ...
Before the form, convert these arrays into classes and give:

'services' => [Doctrine\Common\Collections\ArrayCollection]
   WrapClass
        $name = "Профилактические процедуры"
        $services Doctrine\Common\Collections\ArrayCollection
             Service
             Service
   WrapClass
        $name = "Хиругрические процедуры";
        $services Doctrine\Common\Collections\ArrayCollection
             Service
             Service

Now you just have a collection of services, there will be a collection of categories, and in each category there will be a collection of services.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question