I’ve spent a while looking through the symfony2 docs trying to find a suitable method of doing what I need to do, maybe I’m looking in the wrong place.
Basically, I have an entity called Album, which can have many Subalbums associated with it. As the user is creating an Album entity using a form, I want them to be able to create quick Subalbum entities in-line, which will get saved later on. I also want to display the field in a custom format, so I don’t want to use a select tag with a multiple attribute, instead I’m manually rendering it in Twig (I haven’t had a problem with this).
The relationship on Subalbum is defined as follows:
/**
* @ORM\ManyToOne(targetEntity="Vhoto\AlbumBundle\Entity\Album")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
Here’s what I’ve tried so far…
-
Use an
entitytype field in the form builder, and then manually output the field. The issue I have with using theentityfield is that if the user creates aSubalbuminline, symfony2 doesn’t like it when I submit the form because it has no ID. -
Use a hidden field type and try submitting multiple entries under the same field name (
album[subalbums][]). Symfony2 also doesn’t like this when I submit the form
I guess I’m going to have to have a prePersist method in my Album entity to create any Subalbum entities that the user has created inline?
Hopefully there is a far more graceful solution that I’m just completely overlooking.
Let me know if anything is unclear.
There is indeed a better way to do it.
Entity creation
Create the two
EntityPOPOs and assign amany-to-onerelationship to one of the fields of the child entity (you have done this correctly). You might also want to define aone-to-manyrelationship in the parentI am not sure if it’s necessary, but you should explicitly set the relationship in your setters just to be sure. For example in your owning entity:
Doctrinedoesn’t probably use these methods to bind the post data, but having this for yourself might take care of several persisting issues.Form type creation
Create a form type for both entities
With the options
allow_addandallow_deleteyou’ve effectively told Symfony, that a user can add or remove entities from the collection. Theprototypeoption lets you have a prototype of the so-called sub-form on your page.Controller
You should enforce the relationship here as well just to be safe. I have tucked this away in a separate entity manager layer (I have separate managers for more complicated entities), but you can most certainly do this in your controllers as well.
View
Prepare your form’s template. The prototype should be rendered by calling
form_rest(form)somewhere in your template. In case it doesn’t or you’d like to customize the prototype, here’s a sample of how to do it.You’d have to make the form dynamic by using JavaScript. If you use
jQuery, you can access the prototype by calling$('ParentType_children_prototype').html(). It is important to replace all occurrences of$$name$$in the prototype with the proper index number when adding a new child to the parent.I hope this helps.
EDIT I just noticed there’s an article in the Symfony2 Form Type reference about
CollectionType. It has a nice alternative on how to implement the front-end for this.