php+collection+mapping,php – 当不可能使用@orderBy注释时,基于关联实体的订购原则集合...

我想了解基于相关实体订购原则集合的最佳方法.在这种情况下,不可能使用@orderBy注释.

我在互联网上找到了5个解决方案.

/**

* This method will change the order of elements within a Collection based on the given method.

* It preserves array keys to avoid any direct access issues but will order the elements

* within the array so that iteration will be done in the requested order.

*

* @param string $property

* @param array $calledMethods

*

* @return $this

* @throws \InvalidArgumentException

*/

public function orderCollection($property, $calledMethods = array())

{

/** @var Collection $collection */

$collection = $this->$property;

// If we have a PersistentCollection, make sure it is initialized, then unwrap it so we

// can edit the underlying ArrayCollection without firing the changed method on the

// PersistentCollection. We're only going in and changing the order of the underlying ArrayCollection.

if ($collection instanceOf PersistentCollection) {

/** @var PersistentCollection $collection */

if (false === $collection->isInitialized()) {

$collection->initialize();

}

$collection = $collection->unwrap();

}

if (!$collection instanceOf ArrayCollection) {

throw new InvalidArgumentException('First argument of orderCollection must reference a PersistentCollection|ArrayCollection within $this.');

}

$uaSortFunction = function($first, $second) use ($calledMethods) {

// Loop through $calledMethods until we find a orderable difference

foreach ($calledMethods as $callMethod => $order) {

// If no order was set, swap k => v values and set ASC as default.

if (false == in_array($order, array('ASC', 'DESC')) ) {

$callMethod = $order;

$order = 'ASC';

}

if (true == is_string($first->$callMethod())) {

// String Compare

$result = strcasecmp($first->$callMethod(), $second->$callMethod());

} else {

// Numeric Compare

$difference = ($first->$callMethod() - $second->$callMethod());

// This will convert non-zero $results to 1 or -1 or zero values to 0

// i.e. -22/22 = -1; 0.4/0.4 = 1;

$result = (0 != $difference) ? $difference / abs($difference): 0;

}

// 'Reverse' result if DESC given

if ('DESC' == $order) {

$result *= -1;

}

// If we have a result, return it, else continue looping

if (0 !== (int) $result) {

return (int) $result;

}

}

// No result, return 0

return 0;

};

// Get the values for the ArrayCollection and sort it using the function

$values = $collection->getValues();

uasort($values, $uaSortFunction);

// Clear the current collection values and reintroduce in new order.

$collection->clear();

foreach ($values as $key => $item) {

$collection->set($key, $item);

}

return $this;

}

use Doctrine\Common\Collections\Collection;

public function sort(Collection $objects, $name, $property = null)

{

$values = $objects->getValues();

usort($values, function ($a, $b) use ($name, $property) {

$name = 'get' . $name;

if ($property) {

$property = 'get' . $property;

return strcasecmp($a->$name()->$property(), $b->$name()->$property());

} else {

return strcasecmp($a->$name(), $b->$name());

}

});

return $values;

}

public function getSortedByFoo()

{

$arr = $this->arrayCollection->toArray();

usort($arr, function($a, $b) {

if ($a->getFoo() > $b->getFoo()) {

return -1;

}

//...

});

return $arr;

}

$iterator = $collection->getIterator();

$iterator->uasort(function ($a, $b) {

return ($a->getPropery() < $b->getProperty()) ? -1 : 1;

});

$collection = new ArrayCollection(iterator_to_array($iterator));

5)创建一个服务来收集有序的集合,然后替换无序的集合(我没有一个例子,但我认为很清楚).我认为这是最丑的解决方案.

根据你的经验,哪个是最好的解决方案?您有其他建议,以更有效/优雅的方式订购系列吗?

非常感谢你.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值