简体   繁体   English

Symfony2级联删除

[英]Symfony2 Cascade on Delete

I have two entities: 我有两个实体:

  1. Warehouses 货仓

      id | name --------------------- 1 | Warehouse 1 2 | Warehouse 2 
  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. 我想知道如果从仓库表中删除“仓库1”,如何将Warehouse_id的值设置为Null。 In all actually I need warehouse_id to be set to NULL on all tables in my db if I remove "warehouse 1". 实际上,如果删除了“仓库1”,实际上我需要在数据库中的所有表上将Warehouse_id设置为NULL。

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" 例如,在我的“项目”实体中,我已进行了此设置,而删除“仓库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();
}

You are probably better of keeping it as restrict on delete, then use a doctrine event subscriber to set the reference to null. 您最好将其保留为删除限制,然后使用原则事件订阅者将引用设置为null。

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html 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. 在破坏数据时,请尽量不要让框架自己采取行动,否则,您将开始在应用程序中创建难以诊断的漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM