主义:通过关系发现了一个新实体

时间:2018-09-03 21:01:04

标签: php symfony doctrine many-to-one shopware

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

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($ appointments),但这都不起作用。有人可以帮我吗?正确的方法是什么?最后是约会类:

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

1 个答案:

答案 0 :(得分:1)

我认为问题可能是Offer->provider,当您使用级联持久保存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);