knete
knete

Reputation: 123

How to prevent Deletion by the EntityManager (Doctrine Assocation)

i got the following problem:

i've got an entity Inquiry with an One-To-One, Unidirectional Association to Customer:

/**
 * @var Customer
 *
 * @ORM\OneToOne(targetEntity="Customer", cascade={"persist"}, fetch="EAGER")
 * @ORM\JoinColumn(name="Customer", referencedColumnName="id", onDelete="SET NULL", nullable=true)
 */
protected $customer;

If i delete the Customer via DB Backend (PhpMyAdmin), then anything is fine: the field customer is set to null, but if i delete the Customer object with the EntityManager, then the Inquiry is also deleted, why?

$em = $this->getDoctrine()->getManager();
$em->remove($customer);
$em->flush();

I just want to set it to null.

Sorry for my bad english, i hope somebody can help ;)

Many Greetings

Upvotes: 3

Views: 839

Answers (1)

Anam Shah
Anam Shah

Reputation: 319

You annotate the mapping wrong. Try this

/**
* @ORM\ManyToOne(targetEntity="Customer")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $customer;

Upvotes: 1

Related Questions