如何在内部连接表上应用DISTINCT

时间:2014-05-19 02:01:41

标签: php orm doctrine-orm doctrine dql

我有以下实体,基本上它说图片有商店

class InstagramShopPicture
{

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

}

如何从DQL中的唯一商店查询10张图片?

到目前为止,我有以下内容:

 $query = $em->createQueryBuilder()->select('picture')
               ->from("ShopiousMainBundle:InstagramShopPicture", 'picture')
               ->innerJoin('picture.shop', 'shop')
               ->setMaxResults(10);
                    ;

换句话说我只想要DISTINCT商店,但我发现很难将DISTINCT转换成DQL

更新

$query = $em->createQueryBuilder()
                    ->select('pictures')
                    ->from("AppMainBundle:InstagramShop", 'shop')
                    ->innerJoin('shop.userPictures', 'pictures')
                    ->innerJoin('pictures.category', 'category')
                    ->innerJoin('category.pictureFirstLevelCategory', 'firstLevelCategory')
                    ->distinct()
                    ;

        $query->andWhere('firstLevelCategory = :firstLevelCategory');
        $query->andWhere('shop.isLocked = false');
        $query->andWhere('shop.expirydate IS NOT NULL');
        $query->andWhere('shop.id != :shopId');
        $query->andWhere('shop.expirydate >= :currentDate');

        $parameter["currentDate"] = new \DateTime('now');
        $parameter["shopId"] = $shopId;
        $parameter["firstLevelCategory"] = $picture->getCategory()->getPicturefirstlevelcategory()();

我尝试过以上操作并且它给了我这个错误:

Error: Cannot select entity through identification variables without choosing at least one root entity alias.

更新

SELECT DISTINCT pictures FROM AppMainBundle:InstagramShop shop INNER JOIN shop.userPictures pictures INNER JOIN pictures.category category INNER JOIN category.pictureFirstLevelCategory firstLevelCategory WHERE firstLevelCategory = :firstLevelCategory AND shop.isLocked = false AND shop.expirydate IS NOT NULL AND shop.id != :shopId AND shop.expirydate >= :currentDate 

0 个答案:

没有答案