7/20/2017 - 9:33 AM

Collection updating inverse side form BUG!

Collection updating inverse side form BUG!


by_reference = false, to call addElement(), removeElement()

$builder->addEventListener(FormEvents::POST_BIND, function(DataEvent $event) use ($em) {
    // Inverse relationship that we have to update
    $inverse    = $event->getData();
    // All possible owning-side Entities (very expensive this way, totally a hack)
    $owners     = $em->getRepository('MyBundle:OwnerEntity')->findAll();
    // User-selected collection of owning-side Entities
    $owned      = $inverse->getOwners();

    // Iterate through all possible owners & remove the inverse Entity in question
    foreach ($owners as $owner) {

    // Re-establish the inverse Entity relationship that was just previously removed
    foreach ($owned as $owner) {

    // Yes yes, the two previous for-each loops could be done in a single pass, but I'm just typing crap up!