Working with Symfony 3.x and Doctrine I have this problem: Entity "Foo" is defined as follows:
/**
* @ORM\Entity
* @ORM\Table(name="foo")
*/
class Foo
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $incr_int;
...
}
Leaving aside that $id and $incr_int will (allways?) be the same value I get the following error when creating a new Entity of type Foo:
An exception occurred while executing 'INSERT INTO foo (incr_int) VALUES (?)' with params [null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'incr_int' cannot be null
While this seem to make sense looking at the error itself I dont get how to fix it when creating Foo like this (which I believe is the standard way in Symfony?):
$foo = new Foo();
$em->persist($foo); // $em is the entity manager
$em->flush();
Like I would expect it works if I delete the $incr_int field from Foo because the only remaining field $id is auto generated by increasing the last inserted id-value by 1. I was assuming this behaviour should be the same for the $incr_int field. Well... obviously it's not and I can't figure out why. Any help is highly appreciated.
There were 2 problems in my code (thanks to @nospor).
I could solve this issue by removing the second auto increment condition and creating the Entity with the required fields (here only the auto generated id field) and then flushing it. After flushing I am able to retrieve the id and generate a second field's value holding a calculated value that depends on the id value as well.