Nono
Nono

Reputation: 1103

Doctrine: A new entity was found through the relationship

I know, this is a famous problem, but I don't know how to solve it in my special case. I have the following 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;

    ...
}

This is my current function to create an Offer object:

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

I had to use the merge function, because when I used persist, I got the "A new entity was found through the relationship 'Dashboard\Models\Offer#provider' ..." error. With merge it is working. But now I want to create appointments for the offer. I tried it like this:

...
$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();
...

But with this I'm getting "A new entity was found through the relationship 'Dashboard\Models\Appointment#offer'..." And I also tried it with merge and with collecting all appointments in an ArrayCollection and then using $offer->setAppointments($appointments), but that was neither working. Can someone help me with it? What is the correct way? At the end here is the Appointment 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;
    ...
} 

Upvotes: 2

Views: 1248

Answers (1)

DrKey
DrKey

Reputation: 3495

I think the problem might be Offer->provider which is not configured to be persisted automatically when you persist an instance of Offer using cascade.

Therefore I would probably set cascade persist on it

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

or persist it explicitly in code

$entityManager->persist($provider);

Upvotes: 1

Related Questions