Question

In a legacy system we have a database that saves non existent relations with 0 instead of null. How can I get doctrine to save the parent row with 0 in the related column instead of null?

Example:

Comment-Table

------------------------------------
| id | user_id | comment           |
------------------------------------
| 1    null      this is a comment |
| 2    1         another comment   |
------------------------------------

If I have an entity for that comment table I want it to save user_id = 0 in row 1 if there is no user for this comment. How can I achieve this?

Please consider, that I need this for a legacy system and changing the database schema so that null in that column is possible is no option.

Was it helpful?

Solution

You best bet it so manually insert 0 into user_id just before persisting in your controller.

$comment->setUserId(0);
$em->persist($comment);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top