简体   繁体   English

如何从doctrine查询生成器获取部分结果

[英]How to get partial result from doctrine query builder

I have a product entity in which it has an array as attributes: 我有一个产品实体,其中有一个数组作为属性:

     /**
     * @ORM\OneToMany(targetEntity="Shopious\MainBundle\Entity\ProductPicture", mappedBy="product", cascade={"persist","remove"})
     */
    protected $pictures;

    /** 
    * @Accessor(getter="getCover") 
    */
    private $cover;
    public function getCover()
    {
        if($this->pictures->count() > 0) {
            return $this->pictures[0];
        }
        return new ProductPicture();
    }

Now in my query builder, I have the following code: 现在在我的查询构建器中,我有以下代码:

 $query = $em->createQueryBuilder()->select('p')
            ->from("SiteMainBundle:Product", 'p')
            ->innerJoin('p.category', 'c')
            ->innerJoin('p.shop', 'shop')
            ;

The issue here is that I don't want to be selecting all of p's attribute. 这里的问题是我不想选择p的所有属性。 So I only wanted to get the first ProductPicture in the pictures array (in my case above it's similar to the getCover() method). 所以我只想在图片数组中获得第一个ProductPicture(在我的情况下,它类似于getCover()方法)。 How do I do this? 我该怎么做呢?

So far I can filter out the the partial attributes that I want by doing: 到目前为止,我可以过滤掉我想要的部分属性:

 $query = $em->createQueryBuilder()->select('p.name, p.id')
                ->from("SiteMainBundle:Product", 'p')
                ->innerJoin('p.category', 'c')
                ->innerJoin('p.shop', 'shop')
                ->innerJoin('p.pictures', 'pictures')
                ;

so in the example above I have done inner joined on the pictures, but how do I get the first element from here? 所以在上面的例子中我已经完成了内部加入图片,但我如何从这里获得第一个元素?

In conclusion, my question is how do I select/query the first ProductPicture in the pictures array using the query builder? 总之,我的问题是如何使用查询构建器选择/查询图片数组中的第一个ProductPicture? Because when I do: 因为当我这样做时:

$query = $em->createQueryBuilder()->select('p') $ query = $ em-> createQueryBuilder() - > select('p')

it returns the whole product attributes, but I don't want the whole product attributes.. I only wanted some of them, such as the id, name, etc. However one of the product attributes is actually an entity (which is the ProductPicture), so how do I return this in the select statement? 它返回整个产品属性,但我不想要整个产品属性。我只想要它们中的一些,例如id,name等。但是其中一个产品属性实际上是一个实体(这是ProductPicture ),那么如何在select语句中返回?

EDIT: 编辑:

Here's a SQL equivalent on how the pictures should be inner joined: 这是一个关于图片应该如何内连接的SQL等价物:

SELECT * 
FROM  `product` 
JOIN  `product_picture` ON  `product`.id =  `product_picture`.product_id
WHERE  `product`.id =100
LIMIT 1

Try something like this, if it's a one to many, the normal mySQL behaviour is returning several records with redundant product data, if the same case happens here, then only returning the first record should do the trick. 尝试这样的事情,如果它是一对多,正常的mySQL行为返回多个带有冗余产品数据的记录,如果在这里发生相同的情况,那么只返回第一条记录才能做到这一点。

PS : assuming the ProductPicture entity has a url property that you want to get PS假设ProductPicture实体具有您想要获取的url属性

$query = $em->createQueryBuilder()->select('p.id, p.name, pictures.url')
            ->from("SiteMainBundle:Product", 'p')
            ->innerJoin('p.category', 'c')
            ->innerJoin('p.shop', 'shop')
            ->innerJoin('p.pictures', 'pictures')
            ;

Add a custom repository method with the DQL for your entity, then call it from the controller 使用您的实体的DQL添加自定义存储库方法,然后从控制器调用它

You can name the repository method whatever you want, for this example I'm using findProductWithPicture 您可以根据需要命名存储库方法,对于此示例,我使用的是findProductWithPicture

class ProductRepository extends EntityRepository
{
    /**
     * @param integer $id
     */
    public function findProductWithPicture($id)
    {
        $dql = <<<SQL
SELECT
    p.id    id,
    p.name  name,
    q       picture
FROM
    Shopious\MainBundle\Entity\ProductPicture q,
    Shopious\MainBundle\Entity\Product p
WHERE
    p.id        = :picture_id  AND
    q.product   = p.id
SQL;

        $query = $this->_em->createQuery($dql)->setParameter('picture_id', $id);

        return $query->setMaxResults(1)->getResult();
    }
}

To use this from the controller 从控制器使用它

$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('ShopiousMainBundle:Product')->findProductWithPicture($id);

return $this->render('ShopiousMainBundle:Product:show.html.twig', array(
    'product' => $product[0]
));

In the rendered Twig template, you can access them like so 在渲染的Twig模板中,您可以像这样访问它们

<p>{{ product.id }}
<p>{{ product.name }}
<p>{{ product.picture.whatever_property }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM