学说:通过这种关系找到了一个新的实体

问题描述 投票:2回答:1

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

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;

    ...
}

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

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;
}

我不得不使用合并功能,因为当我使用persist时,我得到了“通过关系找到一个新实体'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();
...

但有了这个,我得到“通过关系找到一个新实体'Dashboard \ Models \ Appointment#offer'......”我还尝试了合并并收集了ArrayCollection中的所有约会,然后使用$ offer- > setAppointments($ appointmentments),但那不起作用。有人可以帮我吗?什么是正确的方法?最后这里是Appointment类:

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;
    ...
} 
php symfony doctrine many-to-one shopware
1个回答
1
投票

我认为问题可能是Offer->provider,当你使用cascade持久保存Offer实例时,它没有被配置为自动持久化。

因此我可能会设置级联持续存在

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

或者在代码中明确地保留它

$entityManager->persist($provider);
© www.soinside.com 2019 - 2024. All rights reserved.