symfony-qb多余查询

cmssoen2  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(382)

我有一系列类似的查询从数据库中获取大量信息。
它们都有如下的基本设置,只是有不同的表。
问题是 $version_ids 它会产生 SELECT * 查询以下3个特定表。

SELECT * FROM prices WHERE version_id = $version_ids[n] // this only for explanation purposes

SELECT * FROM ct_insurance WHERE version_id = $version_ids[n]
SELECT * FROM costs WHERE version_id = $version_ids[n]

因此,如果$version\uids有10个元素,它将生成30个查询,其中每个表10个查询。
这些表的共同点是它们与表版本有一对一的关系,例如:

class Ct_insurance
{

/**

* @ORM\Id
* @ORM\OneToOne(targetEntity="Version", inversedBy="ct_insurance")
* /

private $version;

还要注意的是,即使一个数据库包含上述一系列查询,那些不需要的查询也只生成一次。因此,对于系列中的每个查询,至少没有30个查询。
这是查询的一个示例:

public function getBrandPageData1($version_ids)
{
    $doctrineConfig = $this->getEntityManager()->getConfiguration();
    $doctrineConfig->addCustomStringFunction('field', 'DoctrineExtensions\Query\Mysql\Field');

    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select("v, field(v.id,:ids) as HIDDEN field")
        ->addSelect('b.brand AS brand')
        ->addSelect('b.imgLogoBig')
        ->addSelect('m.id AS model_id')
        ->addSelect('m.model AS model')
        ->addSelect('s.id AS segment_id')
        ->addSelect('s.segment AS segment')
        ->addSelect('v.id AS version_id')
        ->addSelect('v.version AS version')
        ->addSelect('v.places AS places')
        ->addSelect('i.imgPath AS img_path')
        ->addSelect('w.wrYear AS wr_year')
        ->from('AppBundle:Version', 'v')
        ->join('v.model', 'm')
        ->join('m.brandId', 'b')
        ->join('m.segmentId', 's')
        ->join('m.images', 'i')
        ->join('AppBundle:Warranty', 'w', 'WITH', 'w.brand = b.id')
        ->where('v.id IN (:ids)')
        ->orderBy('field')
        ->groupBy('v.id')
        ->setParameter('ids', $version_ids);
    try {
        $query = $qb->getQuery();
        return $query->getResult();
    } catch (\Doctrine\ORM\NoResultException $e) {
        return $e;
    }
}

我试图在dql中生成相同的查询,但在GROUPBY子句中出错。
谢谢

w80xi6nr

w80xi6nr1#

找到了那是什么,或者看起来是这样。
一开始 select ,引用 v 生成实体对象,然后调用所有Map的实体。
这就解决了问题:

$qb->select("field(v.id,:ids) as HIDDEN field")

相关问题