I hit this same question (bug) recently and apparently, you cannot currently persist a proxy document (as per version 1.0.0-beta9). I filed an issue and wrote a simple test case confirming this behavior: https://github.com/doctrine/mongodb-odm/issues/619.
Doctrine Mongodb ODM persist and flush a proxy document
-
29-07-2021 - |
سؤال
I was wondering if it's possible to persist and flush a proxy document.
If we clone a proxy document (don't forget to implement a __clone method to reset the id parameter) and persist and flush will it be insert or not ?
This case might happen if we want copies of documents.
Example documents:
// User.php
class User
{
// @ReferenceOne(targetDocument="Group")
private $group;
}
// Group.php
class Group
{
public function __clone()
{
if ($this->id) {
$this->id = null;
}
}
//....
}
Usage:
$user = $dm->createQueryBuilder('User')->getQuery()->getSingleResult();
$group = $user->getGroup(); // $group is a proxy document
$newGroup = clone $group;
$dm->persist($newGroup);
$dm->flush();
This might be a bad example, but it's just to know if this is possible or not.
Edit: For those of you who want to do something similar, it's not working directly but you can force it by using 'onFlush' events
لا يوجد حل صحيح
نصائح أخرى
لا تنتمي إلى StackOverflow