Reputation: 1282
I have a User entity class, on which I used php bin/console doctrine:generate:entities
the usual way. This class has a unique ID
among its variables :
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
(...)
Much to my surprise, when I call $user=new User();
in my code, the newly created object has its $id
set to NULL
. Given the annotations I gave, isn't Doctrine supposed to set it automatically ?
Sure, I am aware that I can add a setId
method to my class, but I feel this is not the correct way to do things. Any advice appreciated.
Upvotes: 2
Views: 1222
Reputation: 2879
Unique id will be set on $em->flush($user)
. i.e after inserting in table.
You can get more information in doctrine's documentation.
Upvotes: 2