Symfony-doctrine:当我删除他的目标实体时需要将列设置为空

Jea*_*uwy 5 php doctrine symfony doctrine-orm symfony-2.7

我有一个包含此的类区域:

/**
 * @ORM\OneToMany(targetEntity="Planification", mappedBy="zone")
 */
protected $planifications;
Run Code Online (Sandbox Code Playgroud)

和一个类 Planification:

/**
 * @ORM\ManyToOne(targetEntity="Zone", inversedBy="planifications")
 * @ORM\JoinColumn(name="zone_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
 * @Expose
 */
 protected $zone;
Run Code Online (Sandbox Code Playgroud)

但是当我在数据库中查看时,当我删除区域时,zone_id 并未设置为 null。我有任何错误代码。我只看到该区域已被删除,仅此而已。

但如果我以另一种方式配置:“当我删除区域时,我会删除规划”,它会起作用:

在区域类中:

/**
 * @ORM\OneToMany(targetEntity="Planification", mappedBy="zone", orphanRemoval=true, cascade={"remove"})
 */
protected $planifications;
Run Code Online (Sandbox Code Playgroud)

在规划类中:

/**
 * @ORM\ManyToOne(targetEntity="Zone", inversedBy="planifications")
 * @ORM\JoinColumn(name="zone_id", referencedColumnName="id", nullable=true)
 * @Expose
 */
 protected $zone;
Run Code Online (Sandbox Code Playgroud)

但删除区域时我不需要删除规划。我需要将 zone_id 设置为 null。

任何想法 ?

Gre*_*rme 1

You should have

Zone:

/**
* @ORM\OneToMany(targetEntity="Planification", mappedBy="zone")
*/
protected $planifications;
Run Code Online (Sandbox Code Playgroud)

Planifications:

/**
* @ORM\ManyToOne(targetEntity="Zone", inversedBy="planifications")
* @ORM\JoinColumn(name="zone_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* @Expose
*/
protected $zone;
Run Code Online (Sandbox Code Playgroud)

Then use

php bin/console doctrine:schema:update --dump-sql
Run Code Online (Sandbox Code Playgroud)

if this seems correct use:

php bin/console doctrine:schema:update --force
Run Code Online (Sandbox Code Playgroud)

OR:

You could also do something like this I reckon, try it out:

Zone:

/**
* @ORM\OneToMany(targetEntity="Planification", mappedBy="zone", orphanRemoval=true)
*/
protected $planifications;
Run Code Online (Sandbox Code Playgroud)

Planifications:

/**
* @ORM\ManyToOne(targetEntity="Zone", inversedBy="planifications")
* @ORM\JoinColumn(name="zone_id", referencedColumnName="id", nullable=true)
* @Expose
*/
protected $zone;
Run Code Online (Sandbox Code Playgroud)