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?
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 theObjectRepository
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.