Filtering and Sorting a Doctrine Arraycollection
ArrayCollection object has so many useful in-build methods, that makes ArrayCollection superior comparable to normal PHP array. This post is about how to filter and sort an ArrayCollection so output of elements can be in certain order or filtered.
Filter
To filter we can use ArrayColleciton in-build filter method, which takes a predicate and returns all the elements satisfied by the predicate provided, keeping the order of the elements intact. A code example would be nice.
Code Example :
Sort
ArrayCollection doesn't have any in-built sort methods, Though there is a matching method which can work over an ArrayCollection with help of Doctrine\Common\Collections\Criteria, but its limited to one-to-many use cases. A many-to-many association can't enjoy such usage.
Thus below is a snippet that gets Array iterator from ArrayCollection and sorts data accordingly before making a fresh ArrayCollection with sorted elements.
Code Example :
Filter
To filter we can use ArrayColleciton in-build filter method, which takes a predicate and returns all the elements satisfied by the predicate provided, keeping the order of the elements intact. A code example would be nice.
Code Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php /** * @var \Doctrine\Common\Collections\Collection */ $locations; // ArrayCollection() variable that has $location object in its element. // To filter active locations. $activeLocations = $locations->filter(function($location) { return $location->getStatus(); // filter based on status. }); // Now $activeLocations holds all locaitons from $locations those have an active locaiton. ?> |
Sort
ArrayCollection doesn't have any in-built sort methods, Though there is a matching method which can work over an ArrayCollection with help of Doctrine\Common\Collections\Criteria, but its limited to one-to-many use cases. A many-to-many association can't enjoy such usage.
Thus below is a snippet that gets Array iterator from ArrayCollection and sorts data accordingly before making a fresh ArrayCollection with sorted elements.
Code Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $amenities = $entity->getAmenities(); // ArrayCollection data. // Collect an array iterator. $iterator = $amenities->getIterator(); // Do sort the new iterator. $iterator->uasort(function ($a, $b) { return ($a->getPrice() > $b->getPrice()) ? -1 : 1; }); // pass sorted array to a new ArrayCollection. $amenitiesSorted = new \Doctrine\Common\Collections\ArrayCollection(iterator_to_array($iterator)); ?> |
Comments
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
Data Science Training in Bangalore
apple mobile service centre in chennai
big data course malaysia
big data course
Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Data Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
Nice Post
Java Full Stack Web Developer Course in Warangal
online internship | internship in chennai | online internship for students with certificate | bca internship | internship for bca students | sql internship | online internship for btech students | internship for 1st year engineering students