Reputation: 2824
I have two entities:
Warehouses
id | name
---------------------
1 | Warehouse 1
2 | Warehouse 2
Items
id | warehouse_id | name
--------------------------------------
1 | 1 | Item 1
2 | 2 | Item 2
I am wondering how to set the value of warehouse_id to Null if I remove "warehouse 1" from the warehouse table. In all actually I need warehouse_id to be set to NULL on all tables in my db if I remove "warehouse 1".
In my "Items" entity, for example, I have this set up and it does not seem to do anything for me when I remove "warehouse 1"
/**
* @ORM\ManyToOne(targetEntity="WIC\WarehouseBundle\Entity\Warehouse", inversedBy="purchaseOrderLineItemLocation")
* @ORM\JoinColumn(name="warehouse_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* @Common\Versioned
*/
protected $warehouse;
Here is my full warehouse entity
Is there something in here that I need to set?
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\Column(name="name", type="string", length=255)
* @Common\Versioned
* @Assert\NotBlank(message="Location Name cannot be blank.")
*/
private $name;
/**
* @var string $description
*
* @Common\Versioned
* @ORM\Column(name="description", type="text")
*/
protected $description;
/**
* @var string
* @ORM\Column(name="address", type="string", length=255)
* @Common\Versioned
* @Assert\NotBlank(message="Address cannot be blank.")
*/
private $address;
/**
* @var string
* @ORM\Column(name="address2", type="string", length=255, nullable=true)
* @Common\Versioned
*/
private $address2;
/**
* @var string
* @ORM\Column(name="city", type="string", length=255)
* @Common\Versioned
* @Assert\NotBlank(message="City cannot be blank.")
*/
private $city;
/**
* @var string
* @ORM\Column(name="state", type="string", length=255)
* @Common\Versioned
* @Assert\NotBlank(message="State cannot be blank.")
*/
private $state;
/**
* @var string
* @ORM\Column(name="zip", type="string", length=255)
* @Gedmo\Versioned
* @Assert\NotBlank(message="Zip cannot be blank.")
*/
private $zip;
/**
* @var string
* @ORM\Column(name="country", type="string", length=255, nullable=true)
* @Common\Versioned
*/
private $country;
/**
* @var string
* @ORM\Column(name="phone", type="string", length=255, nullable=true)
* @Common\Versioned
*/
private $phone;
/**
* @var string
* @ORM\Column(name="email", type="string", length=255, nullable=true)
* @Common\Versioned
*/
private $email;
/**
* @var string
* @ORM\Column(name="fax", type="string", length=255, nullable=true)
* @Common\Versioned
*/
private $fax;
/**
* @ORM\OneToMany(targetEntity="WIC\InventoryLocationBundle\Entity\InventoryLocation", mappedBy="inventoryLocation")
*/
protected $inventoryLocations;
/**
* @ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
* @Common\Blameable(on="create")
*/
private $createdBy;
/**
* @ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User")
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
* @Common\Blameable(on="update")
*/
private $updatedBy;
/**
* @ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", inversedBy="warehouses")
* @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
* @Common\Versioned
* @Common\Blameable(on="create")
*/
protected $account;
/**
* @var datetime $created
*
* @Common\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
*
* @Common\Timestampable(on="update")
* @ORM\Column(type="datetime", nullable=true)
*/
private $updated;
/**
* @ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
public function __construct()
{
$this->inventoryLocations = new ArrayCollection();
}
Upvotes: 0
Views: 691
Reputation: 6946
You are probably better of keeping it as restrict on delete, then use a doctrine event subscriber to set the reference to null.
http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
This would give you more control over the process, as well as keeping you aware of what your application is doing. Try not to let the framework act on its own when it comes to destruction of data, otherwise you will start creating holes in your application that become difficult to diagnose.
Upvotes: 1