Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Invalid PathExpression. Must be a StateFieldPathExpression

I am new to the Symfony2 query builder, here is what I do:

    $builder
        ->add('access', 'entity', array(
            'label' => 'Behörigheter',
            'multiple' => true,   // Multiple selection allowed
            'expanded' => true,   // Render as checkboxes
            'property' => 'name', // Assuming that the entity has a "name" property
            'class'    => 'BizTV\ContainerManagementBundle\Entity\Container',
            'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) {
                $qb = $er->createQueryBuilder('a');
                $qb->where('a.containerType IN (:containers)', 'a.company = :company');
                $qb->setParameters( array('containers' => array(1,2,3,4), 'company' => $company) );

                return $qb;
            }
        ));     

It works fine except I want to order my entities by containerType (which is a relational field, FK).

When I add this line:

$qb->orderBy('a.containerType', 'ASC');

I get Error: Invalid PathExpression. Must be a StateFieldPathExpression.

So what is this - I can use the relation field containerType in my where clause but not in my sort clause? Or am I missing something else?

like image 918
Matt Welander Avatar asked Aug 11 '12 22:08

Matt Welander


1 Answers

'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) {
      $qb = $er->createQueryBuilder('a');
      $qb->innerJoin('a.containerType', 'ct');
      $qb->where('a.containerType IN (:containers)', 'a.company = :company');
      $qb->orderBy('ct.id', 'ASC'); // Or the field you want from containerType
      $qb->setParameters( array('containers' => array(1,2,3,4), 'company' => $company));

      return $qb;
}

You can't use containerType with sort clause because this an entity in relation with the current ! The query builder don't know the field to use (even containerType represents the id of the entity !). So you need to join the entity and sort by its field manually !

like image 133
Sybio Avatar answered Oct 05 '22 18:10

Sybio