4

当我传递自定义的连接查询时,我遇到了相关捆绑包的问题。我的主要目的是减少在分页 20 个结果时运行的查询,它执行 30 多个查询。

但是,使用预定义的查询与连接其他各种表“通常”减少到 2 个查询,当然,这不会损害性能!

问题出现在 KnpPaginatorBundle 上,似乎我在运行 30 个查询时使用了相同的查询。

控制器

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $form = $this->createForm(new SoundFilterType());
    if (!is_null($response = $this->saveFilter($form, 'sound', 'sound'))) {
        return $response;
    }

    $qb = $em->getRepository('AcmeSoundBundle:Soundtrack')->findAllJoin();
    $paginator = $this->filter($form, $qb, 'sound');


    return array(
        'form' => $form->createView(),
        'paginator' => $paginator,
    );
}

/**
 * @param QueryBuilder $qb
 * @param string       $name
 */
protected function addQueryBuilderSort(QueryBuilder $qb, $name)
{
     $alias = current($qb->getDQLPart('from'))->getAlias();
     if (is_array($order = $this->getOrder($name))) {
         $qb->orderBy($alias . '.' . $order['field'], $order['type']);
     }
}

/**
 * Filter form
 *
 * @param  FormInterface  $form
 * @param  QueryBuilder   $qb
 * @return SlidingPagination
 */
protected function filter(FormInterface $form, QueryBuilder $qb, $name)
{
    if (!is_null($values = $this->getFilter($name))) {
       if ($form->bind($values)->isValid()) {
           $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
       }
    }

    $count = $this->getDoctrine()->getManager()
                ->createQuery('SELECT COUNT(c) FROM AcmeSoundBundle:Sound c')
                ->getSingleScalarResult();

    // possible sorting
    $this->addQueryBuilderSort($qb, $name);
    return $this->get('knp_paginator')->paginate($qb->getQuery()->setHint('knp_paginator.count', $count), $this->getRequest()->query->get('page', 1), 20, array('distinct' => false));

}

询问

public function findAllJoin()
{        
     $qb = $this->createQueryBuilder('q');
     $query = $qb->select('u')
          ->from('AcmeSoundBundle:Sound', 'u')
          ->innerJoin('u.artists', 'a')
          ->leftJoin('u.versions', 'v')
          ->leftJoin('v.instruments', 'i')
          ->addOrderBy('u.dataIns', 'ASC');
}

这是一个要点https://gist.github.com/Lughino/6358896

我不明白为什么它不使用我的查询..

任何想法?

编辑:

对于这个问题,我发现了错误:

public function findAllJoin()
{        
    $qb = $this->createQueryBuilder('q');
    $query = $qb->select('u, a, v, i')
      ->from('AcmeSoundBundle:Sound', 'u')
      ->innerJoin('u.artists', 'a')
      ->leftJoin('u.versions', 'v')
      ->leftJoin('v.instruments', 'i')
      ->addOrderBy('u.dataIns', 'ASC');
}

反而:

public function findAllJoin()
{        
    $qb = $this->createQueryBuilder('q');
    $query = $qb->select('u')
      ->from('AcmeSoundBundle:Sound', 'u')
      ->innerJoin('u.artists', 'a')
      ->leftJoin('u.versions', 'v')
      ->leftJoin('v.instruments', 'i')
      ->addOrderBy('u.dataIns', 'ASC');
}

现在的问题是,如果我选择一列进行排序,我只显示一个结果,即使我排序失败,实际上结果根本没有排序。

有什么我想念的吗?

编辑2

解决了!如果我设置我的两个从 from 实例化并为此搞砸!这样:

public function findAllJoin()
{        
   $qb = $this->createQueryBuilder('u');
   $query = $qb->select('u, a, v, i')
               ->innerJoin('u.artists', 'a')
               ->leftJoin('u.versions', 'v')
               ->leftJoin('v.instruments', 'i')
               ->addOrderBy('u.dataIns', 'ASC');
}

问题已经解决了!

4

0 回答 0