我正在尝试用MongoDB / Doctrine做一个简单的ReferenceOne,但是我 得到一个非常奇怪的问题:
My ID:4e63611cbc347053a2000001
Create the Privilege Object
Check what's the Privilege->User Id
4e63611cbc347053a2000001
Persist it
Create the Event Object
Persist it
Check what's the Event->Privilege->User Id
4e63611cbc347053a2000001/OK/0
Flush it
Get my document id
4e6546d6bc34700605000012
Check what's the Event->Privilege->User Id
4e6546d6bc34700605000013/NOK/0
正如你在这里看到的,我创建了一个Privilege对象,我引用了一个 用户,我坚持认为=>一切都好。不,我创建了一个事件对象,我嵌入了 权限,检查id =>一切都好。
现在我刷新它,用户> id改变了,我不知道为什么会这样 发生的情况。
我尝试删除级联以查看其行为方式,我最终结束了 使用null $ id。
欢迎任何帮助,以了解我做错了什么!
以下是我使用的代码:
echo "My ID:".$identity->getId()."<br />";
echo "Create the Privilege Object<br />";
$privilege = new \Entity\ODM\Event\Privilege();
$privilege->setUser($identity);
$privilege->setRole("admin");
echo "Check what's the Privilege->User Id<br />";
echo $privilege->getUser()->getId()."<br />";
echo "Persist it<br />";
$dm->persist($privilege);
echo "Create the Event Object<br />";
$event = new \Entity\ODM\Event();
$event->setName("My Event 222");
$event->setDate("25/08/2012");
$event->setUrl("myevent222");
$event->addPrivilege($privilege);
echo "Persist it<br />";
$dm->persist($event);
echo "Check what's the Event->Privilege->User Id<br />";
$privs = $event->getPrivileges();
$output = $identity->getId();
$i = 0;
foreach($privs as $priv)
{
if($priv->getUser()->getId() == '4e63611cbc347053a2000001')
$output .= "/OK/".$i."<br />";
else
$output .= "/NOK/".$i."<br />";
$i++;
echo $output;
}
echo "Flush it<br />";
$dm->flush();
echo "Get my document id<br/>";
$id = $event->getId();
echo $id."<br/>";
$privs = null;
$priv = null;
echo "Check what's the Event->Privilege->User Id<br />";
$privs = $event->getPrivileges();
$output = $identity->getId();
$i = 0;
foreach($privs as $priv)
{
if($priv->getUser()->getId() == '4e63611cbc347053a2000001')
$output .= "/OK/".$i."<br />";
else
$output .= "/NOK/".$i."<br />";
$i++;
echo $output;
}
修剪实体:
class Event {
/**
* @Id
*/
protected $id;
/**
* @EmbedMany(targetDocument="\Entity\ODM\Event\Privilege")
*/
protected $privileges = array();
}
class Privilege {
/**
* @Id
*/
protected $id;
/**
* @ReferenceOne(targetDocument="\Entity\ODM\User",
cascade={"persist"})
*/
protected $user;
/**
* @param field_type $user
*/
public function setUser(\Entity\ODM\User $user) {
$this->user = $user;
}
}
答案 0 :(得分:0)
出于某种原因,这里的代码在symfony2下完全正常。
问题来自于我在Zend(修改过的Bisna)中实施教义的方式 - 结束这个问题。