Unrealomega
Unrealomega

Reputation: 107

Doctrine 2 persist failing to insert due to missing ID

While trying to do an insert using with Doctrine 2's persist-flush method, I've been unable to get this to work at all. The metadata is set to generate a value (I've tried AUTO and IDENTITY with no success), but it's still not auto-incrementing. I've also noticed on the database's schema, the "id" column is not set to auto-increment either. The entity in question contains no pre/post-persist functions.

Entity of type XXXX is missing an assigned ID for field '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.

/**
 * @Id 
 * @Column(type="integer")
 * @GeneratedValue
 */
protected $id;

I was wondering if anyone had any insight on this, as I can't seem to find any google or stackoverflow results that helped. Thanks!

Upvotes: 0

Views: 1932

Answers (3)

jerkan
jerkan

Reputation: 705

I fixed this issue by doing a cache clear.

Upvotes: 0

Unrealomega
Unrealomega

Reputation: 107

So, it appears the problem was due to having more than one column with a @GeneratedValue annotation (Even if it's set to NONE). Removing those and leaving the $id with the only one fixed the issue.

Upvotes: 0

Jan Mares
Jan Mares

Reputation: 805

You have to prefix your annotations with @ORM\, like this:

use Doctrine\ORM\Mapping as ORM;

...

/**
 * @ORM\Id 
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue
 */
protected $id;

Upvotes: 0

Related Questions