1

When I want to delete a user, I need NOT to delete from companies. The 2 entities are linked with another which contains user id and company id.

Here is my annotations in the entity in which I link user and company ( could have done it otherwise but there are some other attributes:

@ORM\ManyToOne(targetEntity="Admin\ManagementBundle\Entity\User", inversedBy="uc", cascade={"persist"})
private $user;


@ORM\ManyToOne(targetEntity="Admin\ManagementBundle\Entity\Company", inversedBy="uc", cascade={"persist"})
 private $compani;

When I delete with EmtityManager()->remove($user); it deletes anything related to this user. Couldn't find anything that could help me.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Seyes Radhouene
  • 143
  • 1
  • 10
  • Check your database and see if you have cascading deletes turned on at the database level. The code you've shown shouldn't cause cascading deletes on its own. – Sculper Jul 20 '15 at 16:13
  • Sorry for the late, any instructions on how to do that ? – Seyes Radhouene Jul 21 '15 at 12:00
  • Run the following query via the MySQL command line: `SHOW CREATE TABLE `. You should be able to see any existing constraints. If you see `ON CASCADE DELETE`, that means that the database is enforcing the deletes itself.
    – Sculper Jul 21 '15 at 15:41
  • there is no ON CASCADE DELETE in query result. – Seyes Radhouene Jul 22 '15 at 09:18

1 Answers1

0

Ensure you have CASCADING DELETES turned on. All that is needed in that case is

DELETE FROM MainTable
WHERE PrimaryKey = ???

You database engine will take care of deleting the corresponding referencing records.

Community
  • 1
  • 1
  • I think you've misunderstood the question - his deletes are currently cascading, but he doesn't want them to (at least not in this direction). – Sculper Jul 20 '15 at 16:13
  • he's right, you've misunderstood me, my deletes are currently cascading, and i don't want them to in both directions x) – Seyes Radhouene Jul 21 '15 at 12:01