I’m working with symfony2, I have a table and I want to manually assign the id.
In the documentation of doctrine I’ve found this:
Identifier Generation Strategies
"NONE: Tells Doctrine that the identifiers are assigned (and thus generated)
by your code. The assignment must take place before a new entity is passed to
EntityManager#persist.
NONE is the same as leaving off the @GeneratedValue entirely."
Pay attention to this: “the assignment must take place before a new entity is passed to En..”
How do I achieve that?
I’m doing this:
$empleado->setId("989865446");
but it says:
Entity of type Entity\Empleado 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.
EDIT:
/**
* @var integer $id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
*/
private $id;
and
public function setId($id)
{
$this->id = $id;
}
The code you posted is correct, so the error must have to do with how you’re constructing the object and when persist() is called.
There’s a similar question here:
Set value for the primay key with strategy set to NONE
Are you running the latest versions of Symfony 2/Doctrine 2?
Try making it a required constructor parameter as suggested in the answer above – that should make it impossible for persist() to be called before setting the ID. The error message means just what it says – you have to assign the ID first, only then call persist.
You are creating the object normally, correct? Your code should look like this (here I’m showing how it would work with the ID required by the constructor, but it should work with the setId() approach too):