Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从条令查询生成器中获取部分结果_Php_Sql_Symfony_Doctrine_Query Builder - Fatal编程技术网

Php 如何从条令查询生成器中获取部分结果

Php 如何从条令查询生成器中获取部分结果,php,sql,symfony,doctrine,query-builder,Php,Sql,Symfony,Doctrine,Query Builder,我有一个产品实体,其中有一个数组作为属性: /** * @ORM\OneToMany(targetEntity="Shopious\MainBundle\Entity\ProductPicture", mappedBy="product", cascade={"persist","remove"}) */ protected $pictures; /** * @Accessor(getter="getCover") */

我有一个产品实体,其中有一个数组作为属性:

     /**
     * @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();
    }
现在在我的查询生成器中,我有以下代码:

 $query = $em->createQueryBuilder()->select('p')
            ->from("SiteMainBundle:Product", 'p')
            ->innerJoin('p.category', 'c')
            ->innerJoin('p.shop', 'shop')
            ;
这里的问题是我不想选择p的所有属性。所以我只想得到图片数组中的第一个ProductPicture(在我上面的例子中,它类似于getCover()方法)。我该怎么做

到目前为止,我可以通过执行以下操作过滤掉我想要的部分属性:

 $query = $em->createQueryBuilder()->select('p.name, p.id')
                ->from("SiteMainBundle:Product", 'p')
                ->innerJoin('p.category', 'c')
                ->innerJoin('p.shop', 'shop')
                ->innerJoin('p.pictures', 'pictures')
                ;
在上面的例子中,我对图片进行了内部连接,但是如何从这里得到第一个元素呢

总之,我的问题是如何使用查询生成器选择/查询图片数组中的第一个ProductPicture?因为当我这样做的时候:

$query=$em->createQueryBuilder()->选择('p')

它返回整个产品属性,但我不想要整个产品属性。。我只想要其中的一些,例如id、名称等。但是其中一个产品属性实际上是一个实体(即ProductPicture),那么如何在select语句中返回它呢

编辑:

下面是一个关于图片如何进行内部连接的SQL等价物:

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

为实体添加带有DQL的自定义存储库方法,然后从控制器调用它

您可以随意命名存储库方法,在本例中,我使用的是
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();
    }
}
在渲染的细枝模板中,可以像这样访问它们

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

尝试类似的方法,如果是一对多,mySQL的正常行为是返回多个带有冗余产品数据的记录,如果这里发生相同的情况,那么只返回第一个记录就可以了。

PS:假设ProductPicture实体具有要获取的url属性


你的意思是把
放在哪里
或者如何执行查询?我在问有没有办法将SQL转换成查询生成器?我对将限制转换为查询感到有点困惑。我知道条令中有一个setMaxResults,但它给出了一个不同的结果。什么样的不同结果?喜欢另一幅画吗?或者整行都不一样?我对你的问题投了更高的票,因为这是一个有趣的问题,但基于你尝试的查询生成器和之后提供的SQL,它也是不明确的。如果您能向我们提供一个示例,说明您希望返回哪些数据,就像查询成功执行一样,那么我可以帮助您找出如何构建查询。@Adam-E我再次编辑/更新了问题,因此此查询的问题是它多次返回相同的产品id。我将在原始问题中添加更多细节。您需要首先了解mysql,如果我们有产品p,带有图像img1、img2、img3,并且您进行查询,它将返回3条记录,所有3条记录都有相同的产品p,但每条记录都有一个不同的图像,(p,img1),(p,img2),(p,img3),您应该明确指定您想要的图像,比如main=1的图像或者类似的东西,我理解所有这些,我认为解决方案是分组
<p>{{ product.id }}
<p>{{ product.name }}
<p>{{ product.picture.whatever_property }}
$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')
            ;