T
T
TANK_IST2018-04-19 20:41:49
symfony
TANK_IST, 2018-04-19 20:41:49

How to delete entities that have relationships?

The user needs to be deleted.
5ad8d37e538f8069075638.png
Wrote this code

$notification = $user->getNotificationSettings();
                            $em->remove($notification);

                            $groups = $user->getGroups();
                            foreach ($groups as $value) {
                                $em->remove($value);
                            }

                            $myShops = $user->getMyShops();
                            foreach ($myShops as $value) {
                                $em->remove($value);
                            }

                            $referrals = $user->getReferrals();
                            foreach ($referrals as $value) {
                                $em->remove($value);
                            }

                            $em->remove($user);
                            $em->flush();

I believe that I delete because I get an error
5ad8d4af53704207778515.png
How to delete relations correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
voronkovich, 2018-04-19
@voronkovich

If you want to delete links, use cascade delete (or SET NULL). Something like this:

//* @Entity */
class Group
{
    /**
     * @ManyToMany(targetEntity="User")
     * @JoinTable(
     *     joinColumns={@JoinColumn(onDelete="CASCADE")},
     *     inverseJoinColumns={@JoinColumn(onDelete="CASCADE")}
     * )
     */
    private $users;
}

https://www.doctrine-project.org/projects/doctrine...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question