颠倒Doctrine_Collection的顺序

时间:2012-03-21 10:43:09

标签: php doctrine symfony-1.4

我正在寻找一种彻底改变Doctrine_Collection顺序的方法。 我知道这听起来很奇怪,所以让我解释一下我的(简单)目标:我需要显示x 最新/最新记录,但我必须以相反的顺序显示它:最旧第1次

如果不清楚,这是一个例子: 让我说我在我的表中有这个(让我们称之为'示例'):

id      date
1       2012-01-21
2       2012-03-19
3       2012-02-21
4       2012-03-21

到目前为止,我已经这样做了:

Doctrine::getTable('Example')->createQuery('d')
    ->orderBy('date DESC')
    ->limit(3);

返回

id      date
4       2012-03-21
2       2012-03-19
3       2012-02-21

但我想要那个:

id      date
3       2012-02-21
2       2012-03-19
4       2012-03-21

编辑:

我找到了一个解决方案,使用中间阵列&在上面使用array_reverse。但它看起来并不好:(

这是我写的代码:

    $query = Doctrine::getTable('Example')
            ->createQuery('e')
            ->orderBy('date DESC')
            ->limit(3)
        $collection = $query->execute();

        //Here is the dirty hack:
        $itemArray = array();
        foreach ($collection as $item) {
            $itemArray[] = $item;
        }
        $itemArray = array_reverse($itemArray);
        $orderedCollection = new Doctrine_Collection($doctrineClass);
        foreach($itemArray as $item) {
            $orderedCollection->add($item);
        }
        //OrderedCollection is OK but... come on! There must be a cleaner way to do it

编辑2:来自@Adam Kiss的答案

        $query = Doctrine::getTable('Example')
            ->createQuery('e')
            ->orderBy('date DESC')
            ->limit(3)
        $collection = $query->execute();

        //Here is the **lovely** hack:
        $orderedCollection = new Doctrine_Collection('Example');
        for ($i=($collection->count() - 1); $i>=0;$i--) {
            $orderedCollection->add($collection->get($i));
        }

9 个答案:

答案 0 :(得分:15)

没有中间数组:

for ($i = $collection->count(); $i > 0; $i--) {
    $orderedCollection->add($collection->get($i));
}

希望很好的答案:

您可以将Collection导出到数组并将其反转

$query = Doctrine::getTable('Example')
         ->createQuery('e')
         ->orderBy('date DESC')
         ->limit(3)
$collection = $query->execute();
$collection = array_reverse($collection->toArray());

旧(错)回答:

也许你应该使用

Doctrine::getTable('Example')->createQuery('d')
  ->orderBy('date ASC')
  ->limit(3);

答案 1 :(得分:5)

您似乎必须customize Doctrine_collection在集合类中提供与array_reverse类似的内容,或使用如下的一些hackish方法:

$keys = array_reverse($collection->getKeys());
foreach ($keys as $key) {
    $object = $collection->get($key);
}

答案 2 :(得分:5)

如果您不想使用任何特殊的集合功能,只需按相反的顺序迭代元素(没有任何开销),您可以这样做很容易:

/** @var $results Doctrine_Collection this is your result collection */
$iterator = $results->getIterator();
$item = end($iterator);

do {
  var_dump($item);
} while ($item = prev($iterator));

答案 3 :(得分:0)

也许你应该像这样使用

     ->orderBy('date', 'DESC')

因为在本文档中 http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/query-builder.html orderby的签名需要ASC / DESC作为第二个参数

      public function orderBy($sort = null, $order = null);

答案 4 :(得分:0)

如果你想要一个ArrayCollection对象,另一个扩展@ adam-kiss给出的答案的解决办法就是使用array_reverse的返回值构建一个新的ArrayCollection减少for循环需求的对象。

use Doctrine\Common\Collections\ArrayCollection;

$query = Doctrine::getTable('Example')
    ->createQuery('e')
    ->orderBy('date DESC')
    ->limit(3)
;
$collection = $query->execute();
$orderedCollection = new ArrayCollection(array_reverse($collection->toArray()));

答案 5 :(得分:0)

从它的外观来看,肯定只是从orderby中删除“DESC”。查询以完全按照说明的顺序返回日期降序,并且您希望它按升序返回。

答案 6 :(得分:0)

For Doctrine \ ORM \ PersistentCollection

/** @var Doctrine\ORM\PersistentCollection $posts */
$posts = $category->getPosts();
for ($i = $posts->count()-1; $i >= 0; $i--) {
    $post = $posts->offsetGet($i);
    // ... do some logic
}

答案 7 :(得分:0)

它为我工作。 教义版本:1.2

    $collection = //make doctrine collection;
    $reversed_array = array_reverse($collection->toArray());
    $collection->fromArray($reversed_array);

答案 8 :(得分:0)

use Doctrine\Common\Collections\Criteria;
...

$criteria = Criteria::create();
$criteria->orderBy(['id' => 'DESC']);

return $this->objections->matching($criteria);
相关问题