Symfony2在控制器中按DESC排序

时间:2014-06-02 06:09:33

标签: symfony sql-order-by

我似乎无法想象如何在我的控制器代码中按DESC排序。我有一个具有Post实体的OneToMany的类别实体。

我正在尝试通过DESC对category-> getPosts进行排序,它会自动按ASC排序。

我已经尝试过自定义回购,但我需要按类别发布帖子,使用下面的回购提供给我的所有帖子,而不是特定于该类别的帖子。

我知道这很简单,我完全不知道。如何使用当前设置添加订单?

此查询对我有用:

public function getBlogsByCategory($category)
{
    return $this->createQueryBuilder('post')
        ->leftJoin('post.category','category')
        ->andWhere('category.title = :category')
        ->setParameter('category', $category)
        ->orderBy('post.createdAt', 'DESC')
        ->getQuery()
        ->getResult();
}

控制器

public function showAction($category = null)
{
    $em = $this->getDoctrine()->getManager();

    $category = $em->getRepository('AcmeDemoBundle:Category')
        ->findOneByTitle($category);

    if (!$category) {
        throw $this->createNotFoundException('Unable to find blog posts');
    }

    $posts = $category->getPosts();


    return array(
        'posts'    => $posts,
        'category' => $category
    );
}

类别实体

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @var string
 *
 * @Gedmo\Slug(fields={"title"}, unique=false)
 * @ORM\Column(length=255)
 */
private $slug;

/**
 * @ORM\OneToMany(targetEntity="Post", mappedBy="category")
 */
protected $posts;


public function __construct()
{
    $this->posts = new ArrayCollection();
}

public function __toString()
{
    return $this->getTitle() ? $this->getTitle() :  "";
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set title
 *
 * @param string $title
 * @return Category
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

/**
 * Get title
 *
 * @return string
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set slug
 *
 * @param string $slug
 * @return Category
 */
public function setSlug($slug)
{
    $this->slug = $slug;

    return $this;
}

/**
 * Get slug
 *
 * @return string
 */
public function getSlug()
{
    return $this->slug;
}

/**
 * Add posts
 *
 * @param Post $posts
 * @return Category
 */
public function addPost(Post $posts)
{
    $this->posts[] = $posts;

    return $this;
}

/**
 * Remove posts
 *
 * @param Post $posts
 */
public function removePost(Post $posts)
{
    $this->posts->removeElement($posts);
}

/**
 * Get posts
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getPosts()
{
    return $this->posts;
}

发布实体

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

/**
 * Set category
 *
 * @param \Acme\DemoBundle\Entity\Category $category
 * @return Post
 */
public function setCategory(\Acme\DemoBundle\Entity\Category $category = null)
{
    $this->category = $category;

    return $this;
}

/**
 * Get category
 *
 * @return \Acme\DemoBundle\Entity\Category 
 */
public function getCategory()
{
    return $this->category;
}

发布回购

public function getPosts()
{
    $qb = $this->createQueryBuilder('p')
        ->addOrderBy('p.createdAt', 'DESC');

    return $qb->getQuery()
        ->getResult();
}

2 个答案:

答案 0 :(得分:2)

修改

由于您已经使用自定义仓库,因此您必须修改您的仓库方法,如下所示:

public function getOrderedPostsByCategory($categoryId)
{
    return $this->createQueryBuilder('p')
        ->leftJoin('p.Category','c')
        ->addWhere('c.id = :categoryId')
        ->setParameter('c.categoryId', $categoryId)
        ->orderBy('p.createdAt', 'DESC')
        ->getQuery()
        ->getResult();

}

或第二个选项是在执行getPosts

之前修改实体的return $this->posts;方法并在内存中排序数组

答案 1 :(得分:2)

public function getOrderedPostsByCategory($categoryId)
{
    return $this->createQueryBuilder('p')
        ->leftJoin('p.category','c')
        ->addWhere('c.id = :categoryId')
        ->setParameter('categoryId', $categoryId)
        ->orderBy('p.createdAt', 'DESC')
        ->getQuery()
        ->getResult();

}

1) p.category 将类别换成小写

2)并将p.categoryId更改为setParameter

中的 categoryId