“ make:entity --regenerate”创建不正确的(?)函数

时间:2019-03-05 15:44:30

标签: symfony orm doctrine-orm doctrine symfony4

我目前正在学习Symfony教程,并且已经了解了教义双向关系(对不起,如果我使用的术语错误,我不是英语母语者)。我的模型基于一个广告(一对多),该广告显示了为此广告制作的一系列应用程序(一对多)。因此,应用程序必须链接到广告,因此nullable false

class Application
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Advert", inversedBy="applications")
     * @ORM\JoinColumn(nullable=false)
     */
    private $advert;

    //
}

然后我在自己的Advert类中添加了一个$applications属性:

class Advert
{ 
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="advert")
     */
    private $applications;

    //
}

但是当我使用php bin/console make:entity --regenerate来获取removeApplication()函数时,我得到的代码如下:

public function removeApplication(Application $application): self
{
    if ($this->applications->contains($application)) {
        $this->applications->removeElement($application);
        // set the owning side to null (unless already changed)
        if ($application->getAdvert() === $this) {
            $application->setAdvert(null);
        }
    }

    return $this;
}

当此属性显式设置为$advert时,该函数将应用程序的nullable = false设置为null。我注意到了这种不一致,因为我使用的是Symfony 4,而我正在遵循的教程是基于旧版本的,因此该教程中生成的函数要简单得多,并且无法处理$advert属性。

您知道为什么会发生这种情况以及是否可能在以后的项目中导致错误吗?让我知道您是否需要更多代码来理解问题。

1 个答案:

答案 0 :(得分:0)

在我看来,这确实是一个错误,它们可能无法处理生成器内部的可为空的情况。

也许在关系的“广告”侧尝试orphanRemoval,然后会发生有趣的事情:

class Advert
{ 
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="advert", orphanRemoval=true)
     */
    private $applications;
}