If you add a field to an entity and need autogenerated UUID for that field and you already have autogenerated autoincrement Id then you can do generating before persisting an entity.
use Symfony\Component\Uid\Uuid;
// ...
/**
* @var string
*
* @ORM\Column(name="uuid", type="guid", unique=true)
*/
private $uuid;
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->setDateCreated($this->getDateCreated() ?: (new DateTime()));
$this->uuid = Uuid::v4()->toRfc4122();
}
Also, you may need to modify migrations
public function preUp(Schema $schema) : void
{
$this->addSql('ALTER TABLE table_name ADD uuid CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:guid)\'');
$this->addSql('CREATE UNIQUE INDEX UNIQ_1C1B038BD17F50A6 ON table_name (uuid)');
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('UPDATE table_name SET uuid = UUID()');
$this->addSql('ALTER TABLE table_name CHANGE uuid uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:guid)\'');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX UNIQ_1C1B038BD17F50A6 ON table_name');
$this->addSql('ALTER TABLE table_name DROP uuid');
}
Here is an explanation of why more than one autogeneration thru annotations doesn't work with Doctrine
$unique_id
? – Bunkhouse