Doctrine ODM / PHP / MongoDB - ReferenceOne ID 在刷新时更改或清空
我正在尝试使用 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
正如您在这里所看到的,我创建了一个权限对象,在其中引用了 用户,我坚持一下=>一切都好。不,我创建一个事件对象,我嵌入 里面有权限,查看id =>一切都好。
现在我刷新它,并且 user->id 被更改,我不知道为什么会这样 发生。
我尝试删除级联以查看它的行为方式,但最终结果 $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;
}
}
I'm trying to do a simple ReferenceOne with MongoDB/Doctrine, but I
get a really weird problem:
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
As you can see here, I create a Privilege Object where I reference a
User, I persist it => all ok. No I create a Event Object, I embed the
Privilege in it, check the id => all ok.
Now I flush it, and the user->id gets changed, I have no idea why this
happens.
I tried removing the cascade to see how it behaves and I just end up
with a null $id.
Any help would be welcome to understand what I'm doing wrong!
Here is the code I use:
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;
}
Trimmed Entities:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于某种原因,这里的代码在 symfony2 下运行得很好。
问题来自于我在 Zend (修改后的 Bisna)中实现学说的方式 - 结束这个问题。
For some reason the code here works perfectly fine under symfony2.
The problem is coming from the way I implemented doctrine in Zend (modified Bisna) - Closing this question.