2

有没有办法EntityRepository使用特定标准对 Doctrine 2 对象进行排序,或者如果我想传递特定的排序参数,我是否必须使用 DQL 查询?

4

1 回答 1

5

您可以通过将第二个参数传递给 find___ 方法来返回一组有序的元素,此参数必须是一个关联数组,其中键是您要排序的字段的名称,值是“ASC”或“DESC” .

// query for one product matching be name and price
$product = $repository->findOneBy(array('name' => 'foo', 'price' => 19.99));

// query for all products matching the name, ordered by price
$product = $repository->findBy(
    array('name' => 'foo'),
    array('price' => 'ASC')
);

http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database

当然,如果你想要一个最复杂的行为,你应该实现一个自定义存储库。

于 2012-12-31T15:21:43.423 回答