问题描述
我对 Symfony2 中的 Doctrine2 和两个相关实体有问题.
I have a problem with Doctrine2 in Symfony2 and two relationed entities.
有一个用户实体,可以(不是必须)引用一个包含传记等信息的用户元实体.
There is a user-entity that can (not must) have a usermeta-entity referenced which contains information like biography etc.
usermeta 是可选的,因为用户是由另一个系统导入的,而 usermeta 是在我的应用程序中管理的.
The usermeta is optional because user is imported by another system, while usermeta is managed in my application.
当然我想同时保存两者,以便保存用户必须创建或更新用户元实体.
Of course I want to save both together, so that saving a user must create or update a usermeta-entity.
两者都由名为 aduserid 的列连接(两个表中的名称相同).
Both are joined by a column named aduserid (same name in both tables).
我已经认识到,如果 usermeta 是一个可选引用,那么在这种情况下,拥有方应该是 usermeta,否则学说会加载用户并需要 usermeta 实体 - 但它并不总是存在.
I've recognized that if usermeta is an optional reference the owning-side in this case should be usermeta, otherwise doctrine loads user and needs the usermeta entity - but it's not always there.
请注意 User->setMeta.. 中的注释
Please note the comments in User->setMeta..
/**
* User
*
* @ORMTable(name="user")
* @ORMEntity
*/
class User
{
/**
* @var Usermeta
* @ORMOneToOne(targetEntity="Usermeta", mappedBy="user", cascade={"persist"})
*/
protected $meta;
public function getMeta()
{
return $this->meta;
}
/**
*
* @param Usermeta $metaValue
*/
public function setMeta($metaValue)
{
// I've tried setting the join-column-value here
// - but it's not getting persisted
// $metaValue->setAduserid($this->getAduserid());
// Then I've tried to set the user-object in Usermeta - but then
// it seems like Doctrine wants to update Usermeta and searches
// for ValId names aduserid (in BasicEntityPersister->_prepareUpdateData)
// but only id is given - so not luck here
// $metaValue->setUser($this);
$this->meta = $metaValue;
}
/**
* @var integer
*
* @ORMColumn(name="rowid", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* Get rowid
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @var integer
*
* @ORMColumn(name="ADuserid", type="integer", nullable=false)
*/
private $aduserid;
/**
* Set aduserid
*
* @param integer $aduserid
* @return User
*/
public function setAduserid($aduserid)
{
$this->aduserid = $aduserid;
return $this;
}
/**
* Get aduserid
*
* @return integer
*/
public function getAduserid()
{
return $this->aduserid;
}
// some mor fields....
}
还有 Usermeta 类:
And the Usermeta class:
/**
* Usermeta
*
* @ORMTable(name="userMeta")
* @ORMEntity
*/
class Usermeta
{
/**
* @ORMOneToOne(targetEntity="User", inversedBy="meta")
* @ORMJoinColumn(name="ADuserid", referencedColumnName="ADuserid")
*/
protected $user;
public function getUser()
{
return $this->$user;
}
public function setUser($userObj)
{
$this->user = $userObj;
}
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORMColumn(name="ADuserid", type="integer", nullable=false)
*/
private $aduserid;
/**
* Set aduserid
*
* @param integer $aduserid
* @return User
*/
public function setAduserid($aduserid)
{
$this->aduserid = $aduserid;
return $this;
}
/**
* Get aduserid
*
* @return integer
*/
public function getAduserid()
{
return $this->aduserid;
}
}
控制器代码如下所示:
...
$userForm->bind($request);
if($userForm->isValid()) {
$em->persist($user);
$em->flush();
}
...
推荐答案
阅读我自己的旧问题很有趣,因为我现在第一眼看到了问题..
Reading my own old question is quite fun since I see the problem at first glance now..
当谈到一个解决方案时,我认为学说只能处理名为id"的 ID,但是... aduserid 只是没有标记为 ID,它缺少 Id 注释并且学说不能使用这些字段进行连接列..
When it came to a solution I've thought that doctrine can only handle Ids named "id", but ... aduserid is just not marked as ID, it's missing the Id annotation and doctrine cannot use the fields for the join column..
第二件事,Zdenek Machek 是对的:它必须被标记为可空.
Second thing, Zdenek Machek was right: It has to be marked as nullable.
这篇关于Symfony2 Doctrine2 问题与可选的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!