In this last video in our Symfony2 Form Collection tutorial series, we are going to look at how we can use the data (or lack of it) passed in via our Symfony form to remove records from our database.

Throughout the official Symfony docs, as well as in this video, the logic in use is perhaps not in the best of places. What works for examples and tutorials to get the concept across may not scale so well in a real world project. As such, what is about to follow may be better placed in its own Form Handler or similar as we saw in the REST tutorial series. Or in other words, don't just lump all your logic in your controller because you saw it in this example, as your project will quickly become a mess.

The concept for removing looks quite complicated, but is pretty simple when you walk through it.

As mentioned in the video (1:00), you would likely be reloading existing data from your database using some 'edit' logic, but in our video example we know there is only one record in the db, so we can create our $conference entity with a simple, standard Doctrine query.

We pop that $conference entity on top of our newly created form, thereby populating the form with our hydrated Conference object.

Next, we do something a little different.

    $originalSpeakers = new ArrayCollection();

    foreach ($conference->getSpeakers() as $speaker) {
        $originalSpeakers->add($speaker);
    }

We have already looked in the database for our $conference, and we have used that $conference for populating our form. Why are we now doing extra things with it?

Well, ahead of knowing what's changed between when the form was loaded versus when the form has been submitted back, we need to first take note of what Speaker entities were originally associated with this Conference before anything else happens.

All we are doing here is manually taking a snapshot of what Speaker entities were associated with this particular Conference prior to any form changes.

Then, starting on line 36, we are able to compare the contents of our $originalSpeakers ArrayCollection with the submitted form data. Remember, the form will handle the process of populating our $conference object with the data that was submitted, so if a Speaker is missing from the form, but is in the $originalSpeakers, then we can deduce that this Speaker has been removed from this Conference.

To ensure no orphans are left over, we implicitly call $originalSpeaker->setConference(null); before removing the entity altogether.

As this is a loop, we repeat this same process for each of the $originalSpeakers until we are done.

Video Update

Starting at 7:10 we look at the setConference method and have some logic in there to check if the passed in $conference is of type Conference or null.

You can replace this with:

    /**
     * @param Conference $conference
     * @return $this
     */
    public function setConference(Conference $conference = null)
    {
        $this->conference = $conference;

        return $this;
    }

Same thing, but much more succinct.


Code For This Course

Get the code for this course.

Code For This Episode

Get the code for this episode.

Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.


Episodes in this series

# Title Duration
1 Introduction to the Symfony Form Collection Field Type 06:47
2 Adding New Items To Our Collection 08:48
3 Removing Existing Items from a Symfony Form Collection 07:04
4 Persisting Additions to a Symfony Form Collection 09:40
5 Deleting Records from our Form Collection 11:37