Ali Namdari
Ali Namdari

Reputation: 81

Symfony2 : missing an assigned ID

I have a Place and a Placetype entity. My Place entity have his id attribute set to AUTO:

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

But when I want to persist an object, I got the following error :

Entity of type blabla\Entity\Place is missing an assigned 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.

Do you know how to fix this?

Thanks for your help

Upvotes: 3

Views: 22851

Answers (4)

juanitourquiza
juanitourquiza

Reputation: 2194

I work with symfony 3.2.x If use yml you should add this:

generator:
            strategy: IDENTITY

complete example:

id:
    id:
        type: integer
        nullable: false
        options:
            unsigned: false
        id: true
        generator:
            strategy: IDENTITY

Regards

Upvotes: 0

sonique
sonique

Reputation: 4772

This error can happen when the foreign keys are miss-configured. In that case, you need to use IDENTITY strategy. Example:

one beer has one drinker

beer.id_drinker = drinker.id

BUT HERE :

ALTER TABLE `drinker`
ADD CONSTRAINT `drinker_ibfk_1` FOREIGN KEY (`id`) REFERENCES `beer` (`id_drinker`);

Which is BAD> drinker.id = beer.id_drinker

GOOD:

ALTER TABLE `beer`
ADD CONSTRAINT `beer_ibfk_1` FOREIGN KEY (`id_drinker`) REFERENCES `drinker` (`id`);

Upvotes: 1

Ali Namdari
Ali Namdari

Reputation: 81

It works by replacing this :

/**
 * @var integer $uid
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

to this :

/**
 * @var integer $uid
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 */
private $id;

Upvotes: 5

René Höhle
René Höhle

Reputation: 27325

Try this:

/**
 * @var integer $uid
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

Set the strategy to "IDENTITY"

Upvotes: 1

Related Questions