0

我有两个实体:

  1. 仓库

      id | name           
      ---------------------
      1  | Warehouse 1
      2  | Warehouse 2
    
  2. 项目

      id | warehouse_id   | name
      --------------------------------------
      1  | 1              |  Item 1
      2  | 2              |  Item 2
    

如果我从仓库表中删除“仓库 1”,我想知道如何将warehouse_id 的值设置为 Null。实际上,如果我删除“仓库 1”,我需要在我的数据库中的所有表上将仓库 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();
}
4

1 回答 1

1

您可能最好将其保留为对删除的限制,然后使用学说事件订阅者将引用设置为 null。

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

这将使您能够更好地控制流程,并让您了解您的应用程序正在做什么。在破坏数据时尽量不要让框架自行采取行动,否则您将开始在应用程序中创建难以诊断的漏洞。

于 2013-11-11T09:11:28.040 回答