php Symfony2、Doctrine2、findBy() 顺序不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19449740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:29:46  来源:igfitidea点击:

Symfony2, Doctrine2, findBy() order not working

phpsymfonydoctrine-orm

提问by Lkopo

I created a repository for my Articles entity and I'm trying to get all values ordered by ID DESC. But, I'll get everytime values ordered by id ASC. Here is my ArticleRepository.php:

我为我的文章实体创建了一个存储库,我正在尝试获取按 ID DESC 排序的所有值。但是,我会得到每次按 id ASC 排序的值。这是我的ArticleRepository.php

<?php

namespace Acme\BlogBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ArticleRepository extends EntityRepository
{
    public function findAll()
    {
        return $this->findBy(array(), array('id' => 'DESC'));
    }

    public function findOneBySlug($slug)
    {
        $query = $this->getEntityManager()
                      ->createQuery('
                          SELECT p FROM AcmePagesBundle:Article a
                          WHERE a.slug = :slug
                      ')
                      ->setParameter('slug', $slug);

        try {
            return $query->getSingleResult();
        } catch (\Doctrine\ORM\NoResultException $e) {
            return false;
        }
    }
}

Any ideas?

有任何想法吗?

回答by Ahmed Siouani

The Syntax looks good. This should provide a set of well ordered articles.

语法看起来不错。这应该提供一组有序的文章。

First, make sure the @Entity annotation is set with the right Repository class within the Article Entity,

首先,确保在文章实体中使用正确的 Repository 类设置 @Entity 注释,

/**
 * @ORM\Entity(repositoryClass="...");
 */
class Article
{
    // ...
}

Also, if you want to use native helpers, you've just to call findByfrom the ArticleRepositorywithin your controller,

此外,如果您想使用本机助手,您只需findByArticleRepository控制器内部调用,

 $articles = $this->get('doctrine')
      ->getRepository('YourBundle:Article')
      ->findBy(array(), array('id' => 'DESC'));

回答by Richard Pérez

You don't need to create a query in ArticleRepostory.php for that

您不需要为此在 ArticleRepostory.php 中创建查询

In your controller you can just do:

在您的控制器中,您可以执行以下操作:

$entities = $em->getRepository('YourBundle:Article')->findBy(array(), array( 'id' => 'DESC' ));


->findBy(array(), array( 'id' => 'DESC' )); // Order Works  
->findAll(array(), array( 'id' => 'DESC' )); // Order doesn't work

回答by Richard Pérez

This should be work:

这应该是工作:

public function findAll()
{
    $em = $this->getEntityManager();

    $query = $em->createQuery('
        SELECT *
        FROM AcmePagesBundle:Article a
        ORDER BY a.id DESC
    ');

    return $query->getResult();
}

回答by Matthew Davis

I would tend to do this in my repository (to allow for using the same select DQL in different methods in the repository - especially when you have lots of fetch-joins to include):

我倾向于在我的存储库中执行此操作(以允许在存储库中的不同方法中使用相同的 select DQL - 特别是当您要包含大量 fetch-joins 时):

class FooRepository extends EntityRepository
{
    /**
     * Get the DQL to select Foos with all joins
     *
     * @return string
     */
    public function getSelectDql()
    {
        $dql = '
               SELECT f
                 FROM Entity:Foo f
        ';

        return $dql;
    }

    /**
     * Fetch all foos, ordered
     *
     * @return array
     */
    public function fetchAllOrdered()
    {
        $dql = sprintf(
            '%s %s',
            $this->getSelectDql(),
            'ORDER BY f.id DESC'
        );

        return $this->getEntityManager()
            ->createQuery($dql)
            ->getResult();
    }
}

This should make your repository quite flexible, allow different ordering in different methods (if you need to order them differently) and keep all DB-related code out of your controller.

这应该使您的存储库非常灵活,允许在不同的方法中进行不同的排序(如果您需要以不同的方式对它们进行排序)并将所有与 DB 相关的代码保留在您的控制器之外。

回答by Muhammad Shahzad

Symfony 3.3find by order working example.

Symfony3.3按顺序查找工作示例。

From your controller:

从您的控制器:

public function indexAction(){
     $entityManager = $this->getDoctrine()->getManager();
     $categoryRepository = $entityManager->getRepository(ProductCategory::class); 
     $items = $categoryRepository->findBy(array(), array('id' => 'DESC'));
     ....
}