从存储库获取时,Doctrine 如何在不调用 __construct 方法的情况下创建实体?

2022-01-20 00:00:00 fetch php symfony doctrine-orm entity

我在构造函数中写了下一段代码:

I wrote next code in constructor:

public function __construct()
{
    die('creating entity');
}

当我使用 new 运算符创建实体实例时,例如:

When I create instance of entity with new operator like:

$entity = new Entity();

我看到 creating entity 文本.

但是当我从 repo 中获取实体时:

But when I get entity from repo:

$em->getRepository('AcmeDemoBundle:Entity')->find(1)

Doctrine 创建实体对象而不调用 __construct() 方法,我没有看到 creating entity 文本.

Doctrine create an object of entity without calling __construct() method and I don't see the creating entity text.

谁能解释 Doctrine 在从存储库加载对象时如何创建对象?

Can anybody explain how Doctrine creating an object when loading them from repository?

推荐答案

在不调用构造函数的情况下创建实例 ReflectionClass::newInstanceWithoutConstructor

To create an instance without invoking constructor available with ReflectionClass::newInstanceWithoutConstructor

Doctrine 创建映射实体的实例而不在 DoctrineORMMappingClassMetadataInfo::newInstance()

Doctrine create instances of mapped entities without invoking constructor in DoctrineORMMappingClassMetadataInfo::newInstance()

相关文章