Error: Invalid PathExpression. Must be a StateFieldPathExpression

13,739
'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 !

Share:
13,739
Matt Welander
Author by

Matt Welander

Updated on June 04, 2022

Comments

  • Matt Welander
    Matt Welander almost 2 years

    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?