douwuying4709 2015-07-29 17:01
浏览 22
已采纳

Doctrine ORM Form \ Element \ ObjectMultiCheckbox的性能问题,具有非常大的集合

I have a ManyToMany relationship to an Entity (Fair <-> Hotels) with over 12.000 entries. When I'm using the DoctrineModule\Form\Element\ObjectMultiCheckbox in my form, the application runs out of memory. It seems like the ObjectMultiCheckbox loads every single entity which is saved in the database even though the Fair entity isn't assigned to any Hotel (and vice versa).

Here is the Fair entity with the Hotel relation:

/**
 * @ORM\ManyToMany(targetEntity="Hotel", fetch="EXTRA_LAZY")
 * @ORM\JoinTable(name="fair_core_has_hotel", joinColumns={@ORM\JoinColumn(name="fair_core_id", referencedColumnName="id")})
 */  
private $hotel;

And here is the ObjectMultiCheckbox in the FairForm:

$this->add(array(
    'name' => 'hotel',
    'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
    'options' => array(
        'entity' => 'hotel',
        'unchecked_value' => '',
        'object_manager' => $em,
        'target_class' => 'Customer\Entity\Hotel',
        'label_generator' => function($targetEntity) {
            return "".$targetEntity->getLabel();
        },
        'attributes' => array('required' => false),
    ),
));

Already tried to change the fetch-mode to EXTRA_LAZY but it doesn't make any difference. Also the relation is unidirectional, the Hotel entity doesn't know anything about the Fair entity.

Is there any solution? Or have I done something wrong?

Here are the modules I'm currently using: enter image description here

  • 写回答

1条回答 默认 最新

  • doushang2571 2015-07-30 09:34
    关注

    I think you are misunderstanding the working of the doctrine ObjectMultiCheckbox form element. In the Doctrine2 form element documentation you can read that:

    When the Form gets rendered the findAll method of the ObjectRepository will be executed by default.

    In your case it means it will find all entities of target class 'Customer\Entity\Hotel' so this means all 12.000 records.

    No wonder there are some issues with memory :)

    I think you will have to refactor this part of the code so it will only work with a selection of hotels.

    You can read on how to do this in Example 3: extended version. There they show an example where they configure a find_method that uses a specific method from your repository where you can limit the result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?