Doctrine 2 integrity constraint

If using Doctrine you have probably come across the error message “SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘column_id’ cannot be null” at least once. This error occurs on doctrine persist when using a entity that both has mappings for a ordinary field and an Doctrine relation field (many-to-one, one-to-many) with the same name.

Code example that often generates this kind of error.

Why this error is thrown is because that the “Test” entity is expecting “Test2” reference entity to be used instead of alphanumeric value. So Doctrine can control the integrity of your database operations.

I have read on some forums on the internet that you should avoid using the following mapping together with the relation annotation

And only use the function “setTestMap” to define the relation between your entities

Personally i like to define all my field mappings like the top example, that’s because if someone should view the code i want that person to see all the fields that are existing even if that person is not to familiar with Doctrine. One more thing you get by defining all the fields is when running the command to retrieve the result in a array format you get all fields.

So when i came across this error i found that using the “setTestMap” function often solve the problem without deleting the “mapId” mapping, but some times this won’t work and you have to tweak your set function a little

      

Related Articles

About author View all posts

Martin Lundberg

Traveler. Lifelong learner. Coder.
When i'm not working, i'm planning the next dream travel!

Leave a Reply