【发布时间】:2017-02-10 16:53:57
【问题描述】:
我有这个问题:我有几个扩展一些基础文档的文档。所有这些文档都具有某种注释功能,这意味着用户可以向它们添加注释。因此,我有存储在单独集合中的 Note 文档(它需要可搜索且不能嵌入),并且拥有引用该便笺所属文档的所有者属性。我在用教义建模时遇到了麻烦:
<?php
/** @ODM\MappedSuperclass */
abstract class Base {
/** @ODM\Id */
public $id;
/** @ODM\ReferenceMany(targetDocument="Note", mappedBy="owner") */
public $notes;
}
/** @ODM\Document */
class MyDocument extends Base {
/** @ODM\String */
public $name;
}
/** @ODM\Document */
class Note {
/** @ODM\Id */
public $id;
/** @ODM\ReferenceOne(targetDocument="Base", inversedBy="notes") */
public $owner;
/** @ODM\String */
public $text;
}
$mdoc = new MyDocument;
$note = new Note;
$mdoc->name = 'foo';
$note->text = 'bar';
$mdoc->notes[] = $note;
$note->owner = $mdoc;
$dm->persist($mdoc);
$dm->persist($note);
$dm->flush();
$dm->clear();
$note2 = $dm->find(Note::class, $note->id);
echo $note2->owner->name;
我得到:注意:未定义的属性:Proxies__CG__\Base::$name。在其他情况下,我得到:找不到标识符为“XXX”的“Proxies__CG__\Base”文档。
它显然试图加载 Base 类而不是 MyDocument。如何强制它加载正确的类?我尝试将 Base 设置为 MappedSuperclass,设置鉴别器字段,省略 targetDocument 等。但没有任何东西按预期工作,并以不同的错误/错误行为结束。
这可能吗?
可能相关:
- Weird Doctrine ODM exception when using references together with inheritance
- https://github.com/doctrine/mongodb-odm/issues/442
注意:它们都没有真正的帮助,否则我错过了一些东西。
【问题讨论】:
标签: php inheritance doctrine-orm doctrine-odm bidirectional