I'm having a trouble. When I perform an persist() and flush() in Symfony, I'm getting this:
An exception occurred while executing 'INSERT INTO pedido (emissao, total, cliente_id) VALUES (?, ?, ?, ?)' with params ["2018-01-10", "100.00", "65c4002a-06e2-442b-b1da-61197f73ba3b"]: SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value
My annotation is the same in all entities, but in this one (specifically), Doctrine can not create auto id:
/**
* @var \Ramsey\Uuid\Uuid
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
*/
protected $id;
As the field ID is generated automatically, this error appears to make no sense. I have others entities like Pessoa which works fine with the same annotation.
How can I force Doctrine to "understand" the UUID index?
Currenly Doctrine does not support more than one autogenerated field in single entity. If there are 2 fields Doctrine will generate value for the last one and left first field with default value (null
if not specified).
Since Ramsey uses static methods to generate UUID you should place it's generation where persistent logic is (manager, handler or whatever) or inside entity's constructor:
public function __construct()
{
$this->uuid = Uuid::uuid4()->toString();
}
And remove corresponding annotations:
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
Another solution
Since your second column uses auto increment feature of database (I assume you are using MySQL) you can try to:
AUTO_INCREMENT
option:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With