无效的PathExpression.期望StateFieldPathExpression或SingleValuedAssociationField

adi*_*dit 5 orm symfony 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');
Run Code Online (Sandbox Code Playgroud)

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

[Semantical Error] line 0, col 151 near 'style = :style': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
Run Code Online (Sandbox Code Playgroud)

我有一个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;
    }
Run Code Online (Sandbox Code Playgroud)

这是InstagramShop

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

任何想法为什么?

Bar*_*eod 1

今天,当我寻找完全相同的错误消息时,我发现了你的问题,也与使用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');
Run Code Online (Sandbox Code Playgroud)

你可能需要写:

COUNT(pictures.id) instead of COUNT(pictures)
Run Code Online (Sandbox Code Playgroud)