问题:
我正在开发一个 WordPress Symfony 包并为其创建实体。
我有一个Comment
实体,并$comment->user
映射到一个User
实体。
然而WordPress0
用来代表一个来宾用户。它在 Doctrine 中引起了很多问题,因为 id 为 0 的用户永远不存在。它会导致以下问题:
$comment->getUser()
user_id
当该行是时,可能会引发实体未找到异常0
。$comment->setUser()
不起作用,因为您不能使用null
repensent guest(应该使用0
),并且您也不能使用new User(0)
。
问题:
默认情况下,以下代码将保存null
到user_id
数据库中的列:
$comment->setUser(null);
是否可以将 Doctrine 保存0
(而不是null
)到user_id
列?
或者更好的是,我可以0
在null
处理user_id
列时交换吗?
感谢您的时间。
测试用例:
// 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.
方向:
我正在考虑创建自定义映射类型 http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html