我有两个要单向映射 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 配置为级联持久化,但事实并非如此。我这里有什么问题吗?