学说:通过关系发现了一个新的实体

Doctrine: A new entity was found through the relationship

我知道,这是一个著名的问题,但我不知道在我的特殊情况下如何解决它。我有以下 class:

class Offer extends ModelEntity
{
    /**
     * Primary Key.
     *
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var Provider
     *
     * @ORM\ManyToOne(targetEntity="Dashboard\Models\Provider")
     * @ORM\JoinColumn(name="provider_id", referencedColumnName="id", nullable=false)
     */
    protected $provider;

    /**
     * @var Article
     *
     * @ORM\OneToOne(targetEntity="Shopware\Models\Article\Article", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
     */
    protected $article;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Dashboard\Models\Appointment", mappedBy="offer", cascade={"persist", "remove"})
     */
    protected $appointments;

    ...
}

这是我当前创建优惠对象的函数:

public function createOffer(array $data, Provider $provider)
{
    // An article object is created here with persist and flush (Not my class and function, so I can't edit it)
    $article = $this->resource->create($data);

    $offer = new Offer();
    $offer->setProvider($provider);
    $offer->setArticle($article);

    $this->modelManager->merge($offer);
    $this->modelManager->flush();
    return $offer;
}

我不得不使用merge函数,因为当我使用persist时,我得到了"A new entity was found through the relationship 'Dashboard\Models\Offer#provider' ..."错误。通过合并,它正在工作。但现在我想为优惠创建约会。我这样试过:

...
$offer = new Offer();
$offer->setProvider($provider);
$offer->setArticle($article);

if ($data["appointments"]) {
    foreach ($data["appointments"]["dates"] as $i => $date) {
        $appointment = new Appointment();
        $appointment->setDate(new \DateTime($date));
        $appointment->setStreet($data["appointments"]["streets"][$i]);
        $appointment->setZipcode($data["appointments"]["zipcodes"][$i]);
        $appointment->setCity($data["appointments"]["cities"][$i]);
        $appointment->setOffer($offer);
        $this->modelManager->persist($appointment);
    }
}

$this->modelManager->merge($offer);
$this->modelManager->flush();
...

但是有了这个我得到了 "A new entity was found through the relationship 'Dashboard\Models\Appointment#offer'..." 而且我还尝试了合并并在 ArrayCollection 中收集所有约会然后使用 $offer->setAppointments ($appointments),但这都不起作用。有人可以帮我吗?什么是正确的方法?最后是约会 class:

class Appointment extends ModelEntity
{
    /**
     * Primary Key.
     *
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var Offer
     *
     * @ORM\ManyToOne(targetEntity="Dashboard\Models\Offer", inversedBy="appointments")
     * @ORM\JoinColumn(name="offer_id", referencedColumnName="id", nullable=false)
     */
    protected $offer;
    ...
} 

我认为问题可能出在 Offer->provider,当您使用级联持久化 Offer 的实例时,它没有配置为自动持久化。

因此我可能会在其上设置 cascade persist

/**
 * @var Provider
 *
 * @ORM\ManyToOne(targetEntity="Dashboard\Models\Provider", cascade={"persist"})
 * @ORM\JoinColumn(name="provider_id", referencedColumnName="id", nullable=false)
 */
protected $provider;

或在代码中明确保留它

$entityManager->persist($provider);