0

I am working on a sonata admin panel using symfony 3.3. I have two entities Category and Chalets. They are associated many to many so that One category have many Chalets and Chalet can be associated to many Categories.

I want the functionality in my admin panel to show the Chalets based on Category choosen from dropdown and a button to replicate the same to add another category with Chalets and so on.

The picture below will give you the deep insights. Picture of my current functionality

Right now all the Chalets added in the database are visible in the dropdown I want to show only the selected category Chalets to be shown. Alson, the button(Add more) will replicate the Category and Chalet to repeat the process. Thanks!

1 Answers1

0

What you need is sonata_type_collection field.

It will look a bit different out of the box, but capabilities is exactly what you want.

So, how it works:

  • sonata_type_collection field will include a table inside of your edit form. Every row in this table is another Category.
  • there is possibility to add "Add" and "Delete" buttons, so you could add and delete categories directly from this edit form.
  • second column in this embed table could be "Charlets" field, and it can be editable inline (select2 elements). So you can choose charlets for every category.
  • when you add Category - it's a "create" form, it has only Category dropdown. But when you edit a row inline - it's an "edit" form, it will has also "Charlets" multi select. And options for this multi select can be generated based on chosen Category.

So, all this will work without any custom templates/Ajax calls.

P S. If you still want to customize UI - easier to do it with JavaScript.

Maksym Moskvychev
  • 1,471
  • 8
  • 11
  • I have many to many association with Category and Chalets. I tried 'sonata_type_collection' but it asks to add new Chalets. I do not want to create new Chalets, I have chalets already and just want to associate. The screenshot wold give you the better idea. https://prnt.sc/gjt6ne – Daishy Goyal Sep 11 '17 at 13:52
  • Basically I want chalets and categories to be auto-populated. – Daishy Goyal Sep 11 '17 at 13:55
  • As I suggest here, add sonata_type_collection for Category field, not for Chalet – Maksym Moskvychev Sep 11 '17 at 14:30
  • It is showing an error:- INVALID_MODE 'sonata_type _collection'. see below https://stackoverflow.com/questions/26380592/sonata-admin-manytoone-error-sonata-type-collection-mapping-2 – Daishy Goyal Sep 12 '17 at 09:52