deymaz
deymaz

Reputation: 21

The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called

I'm tying to create one to many relations

A have class

class Interview {

    /**
     * @OneToMany(targetEntity="Question", mappedBy="question")
     */
    private $questions;

    public function __construct() {
        $this->questions = new ArrayCollection();
    }

    public function __toString() {
        return $this->id;
    }

    /**
     * @return Collection|Question[]
     */
    public function getQuestions() {
        return $this->questions;
    }

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    ......
}

another

class Question {

    /**
     * @ManyToOne(targetEntity="Interview", inversedBy="interview")
     * @JoinColumn(name="interview_id", referencedColumnName="id")
     */
    private $interview;

    public function getInterview() {
        return $this->interview;
    }

    public function setInterview(Interview $interview) {
        $this->interview = $interview;
        return $this;
    }

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     */
    private $interview_id;
    ......
}

and Controller for all this

if ($form->isSubmitted() && $form->isValid()) {

        $interview = new Interview();
        $question = new Question();

        $em->persist($interview);

        $question->setInterview($interview);
        $question->setTitle($request->get('title'));
        $em->persist($question);
        $em->flush();

        return $this->redirectToRoute('homepage');
}

i'm receiving an error:

Entity of type AppBundle\Entity\Question is missing an assigned ID for field 'interview_id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

Don't understand what the problem and how to fix it.

Upvotes: 2

Views: 7048

Answers (3)

JessGabriel
JessGabriel

Reputation: 1082

I am sure, its too late to answer but maybe someone else will get this error :-D You get this error when your linked entity (here, the Interview entity) is null.

Of course, you have already instantiate a new instance of Interview.But, as this entity contains only one field (id), before this entity is persited, its id is equal to NULL. As there is no other field, so doctrine think that this entity is NULL. You can solve it by calling flush() before linking this entity to another entity

Upvotes: 0

zalex
zalex

Reputation: 797

It seems like your project config have an error in doctrine mapped part.

If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

Try to see full doctrine config and do some manipulation with

auto_mapping: false to true as example or something else...

Also go this , maybe it will be useful.

Upvotes: 0

vikbert
vikbert

Reputation: 1616

To enforce loading objects from the database again instead of serving them from the identity map. You can call $em->clear(); after you did $em->persist($interview);, i.e.

$interview = new Interview();
$em->persist($interview);
$em->clear();

Upvotes: 0

Related Questions