1

I'm using the Symfony web test case to test my endpoints usually. In this case I just wanted to test a single repository call to persist a new entity so I wanted to avoid using the web test client.

I tested using the web test client first and POSTing to the endpoint will create the entity in the SQLite database using the repository. However if I get the repository class in my test and directly call the creation method it fails on persist() with the exception:

Entity of type MyEntity is missing an assigned ID for field '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.

I've updated the schema using

app/console doctrine:schema:update --force --env=test

But app/console doctrine:schema:validate --env=test still tells me the database is not up to date. This would suggest a problem with the mapping, but if there was a problem with the mapping why would it work in production (which uses MySql) or indeed why would it work if I call it using the web test client?

The doctrine mapping for the ID is:

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var int
 */
private $id;
cezar
  • 11,616
  • 6
  • 48
  • 84
mickadoo
  • 3,337
  • 1
  • 25
  • 38
  • Have you tried `@ORM\GeneratedValue(strategy="AUTO")`? – Darragh Enright Aug 18 '15 at 09:38
  • Although I see from [this answer](http://stackoverflow.com/questions/14022374/the-differences-between-generatedvalue-strategies) that `IDENTITY` is preferred for MySQL and SQLite - so perhaps not... – Darragh Enright Aug 18 '15 at 09:40
  • I've tried removing the strategy (which I think defaults to "AUTO"), explicitly setting it to "IDENTITY" / "AUTO" but none of these approaches worked. – mickadoo Aug 18 '15 at 09:49
  • I posted an answer but deleted it - I noticed that your `Column` annotation did not have a `name` attribute; i.e: `@ORM\Column(name="id", type="integer")`. However, I did a quick test of this on a local codebase; removed the `name` attribute and changed the `strategy` to `IDENTITY` and ran my functional tests (which use mysqlite) and everything seemed to run fine. /shrug – Darragh Enright Aug 18 '15 at 09:49
  • From the doctrine documentation it seems "By default the property name is used" – mickadoo Aug 18 '15 at 09:52

0 Answers0