dongpi2503 2013-09-24 12:00
浏览 19

PDO,内存问题,实体和集合

I am building an abstraction layer for a data provider. Each component of the data has it's own repository which returns collections of entities. The repository has three public methods:

public function fetchAll();    
public function findById($id);
public function find(Query $query);

When the 'fetchAll' method is called, a PDO statement is created and executed. The result of this query is then initialised into a ZF2 HydratingResultSet object so that when it's iterated, each item will be correctly created as an entity by the hydrator.

The problem arises however, when I try to establish how best to 'set' this result set on the collection. So far I've done the following:

  • Set the 'HydratingResultSet' object directly on the collection and 'proxied' the iterator interface methods through to the result set property in the collection. For example:

    protected $hydratingResultSet;
    
    public function current()
    {
        return $this->hydratingResultSet->current();
    }
    
  • Iterated through the result set and added each hydrated entity to the collection. For example:

    foreach ($hydratingResultSet as $entity) {
        $collection->add($entity);
    }
    

The former seems to cope much better with large result sets as the query buffering means that the entities are only being hydrated as the collection is iterated. However, I'm concerned that I've coupled the driver to closely with the collection which may derail some caching strategies. Additionally, it means that I wouldn't ever be able to serialize or un-serialize the collection as the PDO statement obviously doesn't allow for this.

The latter method however, quickly runs out of memory in PHP which large result sets which is understandable. Ideally I'd prefer to use this way of doing things as it'll be a lot better to deal with but the memory issues are blighting me currently.

Could anyone offer any advice on this? Is there a different option to consider or a different way of approaching the problem?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥170 如图所示配置eNSP
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上