The Problem:
I am working on a WordPress Symfony bundle and creating entities for it.
I have a Comment entity, and $comment->user is mapped to an User entity.
However WordPress use 0 to represent a guest user. It cause a lots of problems in Doctrine because the user with id zero never exist. It cause the following issues:
$comment->getUser()might throw an entity not found exception when the row’suser_idis0.$comment->setUser()doesn’t work because you can not usenullto repensent guest (should use0), and you cannot usenew User(0)neither.
The Question:
By default, the following code would save null to user_id column in database:
$comment->setUser(null);
Is it possible to make Doctrine save 0 (instead of null) to the user_id column?
Or even better, can I interchange 0 and null when dealing with the user_id column?
Thank you for your time.
Test Cases:
// if a guest posted a comment, pass null to setUser()
// although the actual value will be saved to user_id column is 0
$guestComment->setUser(null);
// if a comment was posted by a guest, getUser() should return null
// although the actual value returned by user_id column is 0
$guestComment->getUser(); // return null
// if a member posted a comment, pass a User entity to setUser()
$memberComment->setUser(new User());
// if a comment was posted by a member, getUser() should return the User entity
$guestComment->getUser(); // return User entity.
Direction:
I am looking at creating a custom mapping types
http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html
I turn out fixed the problem by creating a custom type:
https://github.com/kayue/WordpressBundle/blob/master/Types/WordPressIdType.php