Symfony 连接查询上的KnpPaginatorBundle排序

Symfony 连接查询上的KnpPaginatorBundle排序,symfony,symfony-2.3,Symfony,Symfony 2.3,当我传递一个连接查询时,我对所讨论的包的种类有一个问题。 我的主要目的是减少在分页20个结果时运行的查询,它执行30多个查询 但是,如果使用预定义的查询来连接其他各种表,“通常”会减少到2个查询,当然,这样可以节省性能,不会有任何影响 问题出现在KnpPaginatorBundle中,其中似乎使用了我运行相同查询的30个查询 控制器 public function indexAction(Request $request) { $em = $this->getDoctrine()-

当我传递一个连接查询时,我对所讨论的包的种类有一个问题。 我的主要目的是减少在分页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');
}
这里有一个要点

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

有什么想法吗

编辑: 对于此问题,我发现了错误:

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

问题解决了

旧帖子,但它救了我一天;)官方文档中仍然缺少如何使用连接。。。