“usort”一个Doctrine \ Common \ Collections \ ArrayCollection?

时间:2022-12-23 06:49:07

In various cases I need to sort a Doctrine\Common\Collections\ArrayCollection according to a property in the object. Without finding a method doing that right away, I do this:

在各种情况下,我需要根据对象中的属性对Doctrine \ Common \ Collections \ ArrayCollection进行排序。如果没有找到立即执行的方法,我会这样做:

// $collection instanceof Doctrine\Common\Collections\ArrayCollection
$array = $collection->getValues();
usort($array, function($a, $b){
    return ($a->getProperty() < $b->getProperty()) ? -1 : 1 ;
});

$collection->clear();
foreach ($array as $item) {
    $collection->add($item);
}

I presume this is not the best way when you have to copy everything to native PHP array and back. I wonder if there is a better way to "usort" a Doctrine\Common\Collections\ArrayCollection. Do I miss any doc?

我认为这不是最好的方法,你必须将所有内容复制到本机PHP数组并返回。我想知道是否有更好的方法来“使用”Doctrine \ Common \ Collections \ ArrayCollection。我想念任何文件吗?

3 个解决方案

#1


73  

To sort an existing Collection you are looking for the ArrayCollection::getIterator() method which returns an ArrayIterator. example:

要对现有Collection进行排序,您要查找返回ArrayIterator的ArrayCollection :: getIterator()方法。例:

$iterator = $collection->getIterator();
$iterator->uasort(function ($a, $b) {
    return ($a->getPropery() < $b->getProperty()) ? -1 : 1;
});
$collection = new ArrayCollection(iterator_to_array($iterator));

The easiest way would be letting the query in the repository handle your sorting.

最简单的方法是让存储库中的查询处理您的排序。

Imagine you have a SuperEntity with a ManyToMany relationship with Category entities.

想象一下,你有一个与类别实体具有ManyToMany关系的SuperEntity。

Then for instance creating a repository method like this:

然后例如创建一个这样的存储库方法:

// Vendor/YourBundle/Entity/SuperEntityRepository.php

public function findByCategoryAndOrderByName($category)
{
    return $this->createQueryBuilder('e')
        ->where('e.category = :category')
        ->setParameter('category', $category)
        ->orderBy('e.name', 'ASC')
        ->getQuery()
        ->getResult()
    ;
}

... makes sorting pretty easy.

...使排序变得非常简单。

Hope that helps.

希望有所帮助。

#2


36  

Since Doctrine 2.3 you can use the Criteria API

从Doctrine 2.3开始,您可以使用Criteria API

Eg:

例如:

<?php

public function getSortedComments()
{
    $criteria = Criteria::create()
      ->orderBy(array("created_at" => Criteria::ASC));

    return $this->comments->matching($criteria);
}

Note: this solution requires public access to $createdAt property or a public getter method getCreatedAt().

注意:此解决方案需要公共访问$ createdAt属性或公共getter方法getCreatedAt()。

#3


11  

If you have an ArrayCollection field you could order with annotations. eg:

如果您有一个ArrayCollection字段,则可以使用注释进行订购。例如:

Say an Entity named Society has many Licenses. You could use

假设一个名为Society的实体有许多许可证。你可以用

/**
* @ORM\OneToMany(targetEntity="License", mappedBy="society")
* @ORM\OrderBy({"endDate" = "DESC"})
**/
private $licenses;

That will order the ArrayCollection by endDate (datetime field) in desc order.

这将按照desc顺序通过endDate(datetime字段)对ArrayCollection进行排序。

See Doctrine documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#orderby

请参阅Doctrine文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#orderby

#1


73  

To sort an existing Collection you are looking for the ArrayCollection::getIterator() method which returns an ArrayIterator. example:

要对现有Collection进行排序,您要查找返回ArrayIterator的ArrayCollection :: getIterator()方法。例:

$iterator = $collection->getIterator();
$iterator->uasort(function ($a, $b) {
    return ($a->getPropery() < $b->getProperty()) ? -1 : 1;
});
$collection = new ArrayCollection(iterator_to_array($iterator));

The easiest way would be letting the query in the repository handle your sorting.

最简单的方法是让存储库中的查询处理您的排序。

Imagine you have a SuperEntity with a ManyToMany relationship with Category entities.

想象一下,你有一个与类别实体具有ManyToMany关系的SuperEntity。

Then for instance creating a repository method like this:

然后例如创建一个这样的存储库方法:

// Vendor/YourBundle/Entity/SuperEntityRepository.php

public function findByCategoryAndOrderByName($category)
{
    return $this->createQueryBuilder('e')
        ->where('e.category = :category')
        ->setParameter('category', $category)
        ->orderBy('e.name', 'ASC')
        ->getQuery()
        ->getResult()
    ;
}

... makes sorting pretty easy.

...使排序变得非常简单。

Hope that helps.

希望有所帮助。

#2


36  

Since Doctrine 2.3 you can use the Criteria API

从Doctrine 2.3开始,您可以使用Criteria API

Eg:

例如:

<?php

public function getSortedComments()
{
    $criteria = Criteria::create()
      ->orderBy(array("created_at" => Criteria::ASC));

    return $this->comments->matching($criteria);
}

Note: this solution requires public access to $createdAt property or a public getter method getCreatedAt().

注意:此解决方案需要公共访问$ createdAt属性或公共getter方法getCreatedAt()。

#3


11  

If you have an ArrayCollection field you could order with annotations. eg:

如果您有一个ArrayCollection字段,则可以使用注释进行订购。例如:

Say an Entity named Society has many Licenses. You could use

假设一个名为Society的实体有许多许可证。你可以用

/**
* @ORM\OneToMany(targetEntity="License", mappedBy="society")
* @ORM\OrderBy({"endDate" = "DESC"})
**/
private $licenses;

That will order the ArrayCollection by endDate (datetime field) in desc order.

这将按照desc顺序通过endDate(datetime字段)对ArrayCollection进行排序。

See Doctrine documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#orderby

请参阅Doctrine文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#orderby