Reputation: 237
I have a Question
entity with a property called code
. I want to set the value of code
equal to id
on default with Doctrine annotations.
This is how I tried but I'm getting an error:
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $code = $id;
Or:
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer", options = {"default": $id})
*/
private $code;
Thanks.
Upvotes: 0
Views: 1224
Reputation: 3566
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
I don't know what is your need but maybe postPersist event is what you want :
postPersist - The postPersist event occurs for an entity after the entity has been made persistent. It will be invoked after the database insert operations. Generated primary key values are available in the postPersist event.
Then in your entity :
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $code;
/** @PostPersist */
public function doStuffOnPostPersist()
{
$this->code = $this->id;
}
It is important to understand that your doctrine entity will have an ID only after having been persisted, thus after the post persist event.
Upvotes: 3