我有两个实体:
仓库
id | name
---------------------
1 | Warehouse 1
2 | Warehouse 2
产品
id | warehouse_id | name
--------------------------------------
1 | 1 | Item 1
2 | 2 | Item 2
我想知道如果从仓库表中删除“仓库1”,如何将warehouse_id的值设置为Null。实际上,如果我删除“仓库1”,我需要在我的数据库中的所有表上将warehouse_id设置为NULL。
例如,在我的“项目”实体中,我有这个设置,当我删除“仓库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;
这是我的完整仓库实体
这里有什么我需要设置的吗?
/**
* @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();
}
答案 0 :(得分:1)
您可能最好将其保留为删除限制,然后使用doctrine事件订阅者将引用设置为null。
http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
这可以让您更好地控制该过程,并让您了解您的应用程序正在做什么。在破坏数据时尽量不让框架自行动作,否则你将开始在应用程序中创建难以诊断的漏洞。