【发布时间】:2021-03-19 09:18:39
【问题描述】:
对不起我的英语。我似乎在 mongo 的教义工作中遗漏了一些东西。我有
/**
* @MongoDB\MappedSuperclass
* @MongoDB\DiscriminatorField("type")
* @MongoDB\DiscriminatorMap({
* "hotel"="App\Document\Attributes\HotelAttributes",
* "apartment"="App\Document\Attributes\ApartmentAttributes",
*/
abstract class AbstractAttribute
{
/**
* @MongoDB\Id
* @MongoDB\UniqueIndex()
*/
protected string $id;
...
}
还有几类后代,比如:
namespace App\Document\Attributes;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document(collection="attributes")
* @MongoDB\InheritanceType("SINGLE_COLLECTION")
* @MongoDB\HasLifecycleCallbacks
*/
class HotelAttributes extends AbstractAttribute
{
/**
* @MongoDB\Field(
* type="int",
* nullable=false,
* name="bed_places",
* )
*/
protected int $bedPlaces;
还有一个引用属性的资源类
class Resources
{
/**
* @MongoDB\ReferenceOne(
* targetDocument=AbstractAttribute::class,
* storeAs="dbRefWithDb",
* orphanRemoval=true,
* cascade={"all"}
* )
*/
protected AbstractAttribute $attributes;
...
}
资源创建正常,但是通过id获取时,属性字段没有加载,直到我尝试直接访问该属性,即:
$resource = $this->dm->getRepository(Resources::class)->find($id);
$resource->getAttributes() will return:
$resource->getAttributes()->getCreated() - 将返回创建日期和所有字段。
我有两个问题:
- 如何让学说自动加载所有数据?
- 如何在我的示例中为抽象类指定存储库?
感谢您的帮助。
【问题讨论】:
-
这能回答你的问题吗? Avoid lazy loading Doctrine Symfony2
-
不,我没有在学说-mongodb-odm =(
标签: php mongodb symfony doctrine-odm