【发布时间】:2013-08-09 09:42:39
【问题描述】:
我有两个要单向映射 OneToOne 的实体(User 和 UserPreferences)。
代码如下所示:
/**
* @ORM\Table("users")
* @ORM\Entity
*/
class User
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
*/
protected $id;
...
/**
* @ORM\Column(name="user_preferences_id", type="integer")
* @ORM\OneToOne
* (
* targetEntity="UserPreferences",
* cascade={"persist"}
* )
*/
protected $userPreferences;
public function __construct() {
$this->userPreferences = new UserPreferences();
}
}
/**
* @ORM\Table("user_preferences")
* @ORM\Entity
*/
class UserPreferences extends UserPreferencesEntity
{
/**
* @ORM\Id
* @ORM\Column(name="user_id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
}
现在当一个新用户被创建时,userPreferences 被一个新的 UserPreferences 对象初始化。当试图坚持user 时,Doctrine 抛出一个异常,声称
A new entity was found through the relationship '...\Entity\User#userPreferences' that was not configured to cascade persist operations for entity: ...\Entity\UserPreferences@000000003ae25e5700000000a6eaafc9. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).
但是我还应该做什么呢? User#userPreferences 配置为级联持久化,但事实并非如此。我这里有什么问题吗?
【问题讨论】:
-
没有那个注解属性你试过了吗?
-
没有哪个注解属性?
标签: php mysql orm doctrine-orm