Albert Casadessús
Albert Casadessús

Reputation: 294

Custom query or specific getter method for sonata_type_collection form type

I have a Trip entity that has many HoneymoonComponent. In the TripAdmin, I am using a sonata_type_collection to edit inline all the HoneymoonComponent related:

$formMapper
        ->add('isHoneymoonEnabled', null)
        ->add('honeymoonComponents', 'sonata_type_collection', array('by_reference' => false), array(
                    'edit' => 'inline',
                    'inline' => 'table',

    ));

This works properly, but what I would like to achieve, is to only show the honeymoonComponents that match a criteria (for example: HoneymoonComponent::enabled = false).

I can not find a way to specify a custom query for that, and I have a workaround which I don't know if it's good which is override the HoneymoonComponent::getHoneymoonComponent to only return the ones that match the criteria... but I don't much like it.

Is there a way to specify which "getter" method should sonata_type_collection to use? (or specify custom query). Both solutions would be fine for me.

Thanks in advance !

Upvotes: 4

Views: 2743

Answers (1)

Ioana Hazsda
Ioana Hazsda

Reputation: 580

You can try this:

$formMapper
  ->add('isHoneymoonEnabled', null)
  ->add('honeymoonComponents', null, array(
            'by_reference' => false,
            'class' => 'My\Bundle\Entity\MyEntity',
            'query_builder' => $this->modelManager->createQuery('My\Bundle\Entity\MyEntity', 'h')
                    ->where('h.enabled = 0'),
        ), array(
            'edit' => 'inline',
            'inline' => 'table',
  ));

Upvotes: 2

Related Questions