Doctrine ODM / PHP / MongoDB - ReferenceOne ID changed or nulled when flushed
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 />";
$privi开发者_如何转开发lege = 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;
}
}
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.
精彩评论