2015-06-03 112 views
1

我有这样的学说结构。学说2:没有水合计数实体的相关实体

/** 
* @ORM\Entity 
* @ORM\Table(name="entity_deviceProfile") 
*/ 
class DeviceProfile 
{ 
    /** 
    * @ORM\ManyToMany(targetEntity="TranscodeProfile", inversedBy="deviceProfiles") 
    * @ORM\JoinTable(name="entity_deviceProfile_rel_transcodeProfiles_transcodeProfile") 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    protected $transcodeProfiles = null; 
} 

我希望有方法getTranscodeProfileCount()DeviceProfile类。

所以我添加的功能,以DeviceProfile

public function getTranscodeProfilesCount() { 
    if (!$this->transcodeProfiles->isInitialized()) { 
     $this->transcodeProfiles->initialize(); 
    } 
    return $this->transcodeProfiles->count(); 
} 

它工作正常,除了内存的使用情况和性能。保湿整个收藏需要大量的记忆和时间。我有成千上万的transcodeProfiles,这个数字还在增长。

有没有办法在不保湿收藏的情况下获得记录数? PS:我知道我可以通过运行count()查询$em从实体以外做到这一点。但不幸的是,这个功能是巨大的应用程序的一部分,更新所有的函数调用是一项巨大的工作。所以我正在寻找解决现有功能问题的方法。

回答

1

原理2的一个特征叫做'Extra Lazy Associations',它解决了我的问题。

这正是我所需要的。对于计数而言,它并不会保存收集,而是执行count()查询。

Doctrine filters一起,它也允许无缝地过滤关联的实体。