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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程