无效的PathExpression。期望StateFieldPathExpression或SingleValuedAssociationField

时间:2014-01-20 04:59:15

标签: symfony orm doctrine-orm

我有以下查询:

$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');

由于某种原因,它给了我以下错误:

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

我有一个InstagramShop,与InstagramShopPictures有很多关系:

这是实体:

    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;
    }

这里是InstagramShop

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

任何想法为什么?

1 个答案:

答案 0 :(得分:0)

今天我在寻找完全相同的错误消息时发现了您的问题,这也与使用COUNT()有关。 Pieter Vogelaar帮我解决了以下问题:

$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');

你可能需要写: COUNT(pictures.id)代替COUNT(图片)