Symfony2, Doctrine2, findBy() order not working

时间:2021-04-26 06:47:32

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:

我为我的article实体创建了一个存储库,我正在尝试获取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?

什么好主意吗?

5 个解决方案

#1


13  

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,

首先,确保在Article实体中使用正确的存储库类设置@Entity注释,

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

Also, if you want to use native helpers, you've just to call findBy from the ArticleRepository within your controller,

另外,如果你想要使用本机helper函数,你只需从控制器中的ArticleRepository调用findBy,

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

#2


3  

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

#3


1  

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

#4


0  

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—特别是当您有许多要包含的获取连接时):

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.

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

#5


0  

Symfony 3.3 find by order working example.

Symfony 3.3按订单查找工作示例。

From your controller:

从你的控制器:

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

#1


13  

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,

首先,确保在Article实体中使用正确的存储库类设置@Entity注释,

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

Also, if you want to use native helpers, you've just to call findBy from the ArticleRepository within your controller,

另外,如果你想要使用本机helper函数,你只需从控制器中的ArticleRepository调用findBy,

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

#2


3  

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

#3


1  

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

#4


0  

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—特别是当您有许多要包含的获取连接时):

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.

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

#5


0  

Symfony 3.3 find by order working example.

Symfony 3.3按订单查找工作示例。

From your controller:

从你的控制器:

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