Gauthier
Gauthier

Reputation: 1266

Entity deleted when unlinked from relation via Form EntityType Symfony3.4

I'm using Symfony3.4 with softDeletable module in some entities.

I have a ZoneMaterial entity with two arrayCollection of entities :

/**
 * @ORM\ManyToMany(targetEntity="EPC", cascade={"persist"}, orphanRemoval=true)
 * @ORM\JoinTable(name="app_zone_material_epc",
 *      joinColumns={@ORM\JoinColumn(name="zoneMaterial_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="epc_id", referencedColumnName="id")}
 *      )
 */
private $epcs;

/**
 * @ORM\ManyToMany(targetEntity="EPI", cascade={"persist"}, orphanRemoval=true)
 * @ORM\JoinTable(name="app_zone_material_epi",
 *      joinColumns={@ORM\JoinColumn(name="zoneMaterial_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="epi_id", referencedColumnName="id")}
 *      )
 */
private $epis;


[...]


public function addEpc(EPC $e)
{
    $this->epcs[] = $e;
}

public function removeEpc(EPC $e)
{
    $this->epcs->removeElement($e);
}

public function getEpcs()
{
    return $this->epcs;
}

public function addEpi(EPI $e)
{
    $this->epis[] = $e;
}

public function removeEpi(EPI $e)
{
    $this->epis->removeElement($e);
}

public function getEpis()
{
    return $this->epis;
}

I link/unlink them with a form defined as followed :

    ->add('epcs', EntityType::class,        array(  'label'               =>'MPC',
                                                    'class'               => 'AppBundle\Entity\EPC',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    },
                                                    'multiple'            => true,
                                                    'expanded'            => true))
    ->add('epis', EntityType::class,        array(  'label'               =>'EPI',
                                                    'class'               => 'AppBundle\Entity\EPI',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    },
                                                    'multiple'            => true,
                                                    'expanded'            => true))

And my updateAction function in my controller looks like :

public function updtZoneMaterialAction(Request $request, $id){

    $doctrine       = $this->getDoctrine();
    $entityManager = $doctrine->getManager();
    $zoneMaterial   = $doctrine->getRepository(ZoneMaterial::class)->find($id);

    $form = $this->createForm(ZoneMaterialType::class, $zoneMaterial);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        //Persist informations
        $entityManager->persist($zoneMaterial);
        $entityManager->flush();

        //->redirection
    }

    //Render view
    return $this->render('AppBundle:Zone:updt_zone_material.html.twig', array(
        'form'  => $form->createView(),
        'id'    => $id
    ));
}

The softDeletable extension is installed on my two entities EPI and EPC but it seems that each time I "unlink" an EPI or an EPC from a ZoneMaterial, the concerned entity is remove (with a deletedAt set to new DateTime("NOW")).

Upvotes: 0

Views: 170

Answers (1)

Stratadox
Stratadox

Reputation: 1311

each time I unlink an EPI or an EPC from a ZoneMaterial, the concerned entity is removed

This is what you've told doctrine by configuring orphanRemoval=true.

The setting for orphan removal tells Doctrine whether or not it should remove the entities that got unlinked from their aggregate root.

This concept is very powerful and enables a form of programming that considers persistence as an implementation detail. Rather than manually telling the ORM than an entity must be removed, one can model their domain in such a way that simply removing the entity from the collection is enough to automatically synchronise the database to reflect the state of the model.

If that is not what you want, you should not set orphanRemoval to true.

Upvotes: 1

Related Questions