Tsounabe
Tsounabe

Reputation: 2184

SonataAdmin sonata_type_model with lot of rows throw OutOfMemoryException

I have a simple one to many relation Listing -> Booking with many thousands listings. When i add the following SonataAdmin class :

class BookingAdmin extends Admin
{
...

   $formMapper
     ->add(
        'listing',
         null,
         array(
            'disabled' => true,
         )
     ),
...

An OutOfMemoryException is thrown due to the lot of numbers of listings. I would like to know how to avoid this error by displaying the listing title in the form without using the choice list.

Upvotes: 1

Views: 191

Answers (2)

Tsounabe
Tsounabe

Reputation: 2184

I found an another solution than Yonel one. In this solution we only get the current Listing of the persisted Booking entity in the choice widget. It is only useful if the Listing must not be changed.

class BookingAdmin extends Admin
{
    ...

    protected function configureFormFields(FormMapper $formMapper)
    {
         $listing= $this->getSubject();

         $formMapper
            ->add(
                'listing',
                'sonata_type_model',
                 array(
                    'query' => $this->modelManager
                        ->getEntityManager('Bundle:Listing')
                        ->getRepository('Bundle:Listing')
                        ->find(
                            $listing->getId()
                        ),
                    'disabled' => true,
                )
            );
          ...

Upvotes: 0

yceruto
yceruto

Reputation: 9585

You could use a 'sonata_type_model_autocomplete' form type for these cases (Ref.):

class BookingAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        // the dropdown autocomplete list will show only Booking
        // entities that contain specified text in "title" attribute
        $formMapper->add('listing', 'sonata_type_model_autocomplete', array(
            'property' => 'title'
        ));
    }
}

This one avoids to query all rows to populate the widget.

Upvotes: 1

Related Questions