为什么Doctrine \ ORM \ Configuration" DoctrineProxies"对象包含Universe?

时间:2016-05-06 15:57:08

标签: php symfony orm doctrine-orm proxy-classes

在我的ORM代码中,我有一个实体,其字段被罚款如此:

//part of entity class Item:
/** @Column(name="product_id", type="integer") */
private $productId;

然后我执行了这段代码:

//3 lines ~straight out of Doctrine configuration to get EntityManager
include 'config/doctrine-config.php';
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);

//my own code to retrieve an entity instance:
$instance = $em->find(Item::class, 2);
print_r($instance);

这是我得到的输出(跳过其他一些类似的属性):

Application\Entity\Item Object
(
    [id:Application\Entity\Item:private] => 2
    [description:Application\Entity\Item:private] => Product Kit
    [productId:Application\Entity\Item:private] => -1
)

请注意,print_r()函数中有6(6)行。

一切都很好,直到

接下来,我已将$productId列更改为ManyToOne实体类上的Item关系,如下所示:

/** 
 * @ManyToOne(targetEntity="Product", inversedBy="id")
 * @JoinColumn(name="product_id", referencedColumnName="id")
 */
private $productId;

我运行了相同的代码。

OUT CAME 2,392,600 LINES的宇宙,什么?

print_r输出的两百万,三十九,二千,六百行行。

查看打印输出我看到DoctrineProxies\__CG__\Application\Entity\Product对象包含由2,392,564打印的print_r

问题:

这个对象究竟是什么?为什么它在打印出来时会占用近300Mb的磁盘空间呢?

我不禁想知道这种复杂性是否容易导致每天的代码出现性能问题。例如,我不打算在我的每日代码中打印$instance变量的内容,但我确实返回来自方法调用的大量内容。这是否意味着它是从上面的$em->find(Item::class, 2);调用传递的300Mb变量?

(非常)部分列表

Application\Entity\Item Object
(
 [id:Application\Entity\Item:private] => 2
 [description:Application\Entity\Item:private] => Product Kit
 [ProductId:Application\Entity\Item:private] => DoctrineProxies\__CG__\Application\Entity\Product Object
  (
   [__initializer__] => Closure Object
    (
     [static] => Array
      (
       [entityPersister] => Doctrine\ORM\Persisters\Entity\BasicEntityPersister Object
        (
         [class:protected] => Doctrine\ORM\Mapping\ClassMetadata Object
          (
           [name] => Application\Entity\Product
           [namespace] => Application\Entity
           [rootEntityName] => Application\Entity\Product
           [inheritanceType] => 1
           [generatorType] => 5
           [fieldMappings] => Array
            (
             [id] => Array
              (
               [fieldName] => id
               [type] => integer
               [scale] => 0
               [length] => 
               [unique] => 
               [nullable] => 
               [precision] => 0
               [columnName] => id
               [id] => 1
              )
           [fieldNames] => Array
            (
             [id] => id
             [description] => description
            )

       [columnNames] => Array
            (
             [id] => id
             [description] => description
            )

       [idGenerator] => Doctrine\ORM\Id\AssignedGenerator Object
           [reflClass] => ReflectionClass Object
            (
             [name] => Application\Entity\Product
            )

       [namingStrategy:protected] => Doctrine\ORM\Mapping\DefaultNamingStrategy Object
           [instantiator:Doctrine\ORM\Mapping\ClassMetadataInfo:private] => Doctrine\Instantiator\Instantiator Object

          )

     [conn:protected] => Doctrine\DBAL\Connection Object
          (
           [_conn:protected] => Doctrine\DBAL\Driver\PDOConnection Object
            (
            )

       [_config:protected] => Doctrine\ORM\Configuration Object
            (
             [_attributes:protected] => Array
              (
               [metadataCacheImpl] => Doctrine\Common\Cache\ArrayCache Object
                (
                 [data:Doctrine\Common\Cache\ArrayCache:private] => Array
                  (
                   [dc2_b1e855bc8c5c80316087e39e6c34bc26_[Application\Entity\Item$CLASSMETADATA][1]] => Array
                    (
                     [0] => Doctrine\ORM\Mapping\ClassMetadata Object
                      (
                       [name] => Application\Entity\Item
                       [namespace] => Application\Entity
                       [rootEntityName] => Application\Entity\Item
                       [customGeneratorDefinition] => 
                       [customRepositoryClassName] => 
                       [isMappedSuperclass] => 
                       [isEmbeddedClass] => 
                       [parentClasses] => Array

    [BAZILLION LINES redacted for brevity]

2 个答案:

答案 0 :(得分:3)

您无法在没有XDebug或类似工具(限制转储对象大小)的情况下转储代理对象。

问题非常非常简单:

代理 - >引用EntityManager - >引用UnitOfWork - >包含代理

这显然会导致递归数据结构转储,这会导致在您尝试转储它时没有明显限制的情况下会出现问题。

答案 1 :(得分:1)

DoctrineProxies\__CG__\Application\Entity\Product 是一个代理类...这意味着doctrine实际上不会从数据库中获取实体(用于性能),除非需要它(即调用$product->getName()那些代理类与彼此在递归循环中并且是非常的你看到的大...你不需要的大部分信息,除非你深入潜水......你不应该在新的symfony 2.7+中使用print_r ...我认为有一个功能在调试模式下调用dump() ...如果你使用它来打印实体它有循环保护它只显示参考数字...你也可以使用\Doctrine\Common\Util\Debug::dump()也会打印出更小的列表比2 ^ 234234234行...