Question

I'm facing an error when building an form item using an entity type with a custom query. Without the custom query it worked fine... so I suppose that the problem lies somewhere in the query.

That's the excerpt from type.php

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('Event', 'entity', array(
                'class' => 'EventBundle:Event',
                'query_builder' => function(EntityRepository $er){
                    return $er->createQueryBuilder('e')
                        ->select('e.id, g.title')
                        ->leftJoin('e.Group g')
                        ->orderBy('e.start', 'DESC');
                },
                'property' => 'title',
                'empty_value' => 'Please choose an event...'
            ))

I can see that the query works fine... this are the values that create the exception.

PropertyAccessor ->readPropertiesUntil ('77', object(PropertyPath), '1') in /vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 49

Thanks for any help

Was it helpful?

Solution

The "query_builder" closure option expects an object or an array as return. Your QueryBuilder selects the Id of the Event and the title of the Group, which are not objects. Try this:

$builder
        ->add('Event', 'entity', array(
            'class' => 'EventBundle:Event',
            'query_builder' => function(EntityRepository $er){
                return $er->createQueryBuilder('e')
                    ->select('e')
                    ->leftJoin('e.Group', 'g')
                    ->orderBy('e.start', 'DESC');
            },
            'property' => 'title',
            'empty_value' => 'Please choose an event...'
        ))

OTHER TIPS

You have an error on your Qb Statement

The definition of a leftJoin is done in this way:

// Example - $qb->leftJoin('u.Phonenumbers', 'p', 'WITH', 'p.area_code = 55')
public function leftJoin($join, $alias = null, $conditionType = null, $condition = null);

in your case you need to change:

->leftJoin('e.Group g')

to

->leftJoin('e.Group', 'g')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top