'Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected

I have the following query:

$query = $em->createQueryBuilder()->select('s', 'COUNT(pictures) AS HIDDEN items')
                  ->from("MainBundle:InstagramShop", 's')
                  ->innerJoin('s.userPictures', 'pictures')
                  ;

      $query->andWhere('s.id > :shopId');
      $query->andWhere('pictures.style = :style');
      $query->andHaving('items >= 4');

and for some reason it gives me the following error:

[Semantical Error] line 0, col 151 near 'style = :style': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

I have an InstagramShop which has a many to one relationship with InstagramShopPictures:

Here's the entity:

    class InstagramShopPicture
    {

          /**
         * @Exclude()
         * @ORM\OneToMany(targetEntity="App\MainBundle\Entity\InstagramPictureStyle",         
           mappedBy="picture", cascade={"persist","remove"})
         */
         protected $style; 


        /**
         * @Exclude()
         * @ORM\ManyToOne(targetEntity="InstagramShop", inversedBy="userPictures")
         * @ORM\JoinColumn(name="shop_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
         */
        protected $shop;
    }

and here's InstagramShop

class InstagramShop
{
     /**
     * @Exclude()
     * @ORM\OneToMany(targetEntity="InstagramShopPicture", mappedBy="shop", cascade={"persist"})
     * @ORM\OrderBy({"created" = "DESC"})
     */
    protected $userPictures;
}

any idea why?



Solution 1:[1]

Today I found your question when I was looking for the exact same error message, also related to using COUNT(). Pieter Vogelaar helpt me to solve it the following way:

$qb = $this->createQueryBuilder('c', 'bc')
        ->select('c')
        ->leftJoin('c.books', 'bc')
        ->addSelect('COUNT(bc.id) AS book_count')
        ->where('c.owner = :user')->setParameter(':user', $user)
        ->groupBy('c.id')
        ->orderBy('c.created');

You probable need to write: COUNT(pictures.id) instead of COUNT(pictures)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1