KnpPaginatorBundle对连接查询进行排序

时间:2013-08-27 21:00:53

标签: symfony symfony-2.3

当我传递自定义的连接查询时,我遇到了有问题的捆绑问题。 我的主要目的是减少在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');
}

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

我有什么遗失的吗?

EDIT2

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

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');
}

问题解决了!

0 个答案:

没有答案