NotionCommotion Posted April 9, 2019 Share Posted April 9, 2019 Hoping to get some thoughts on how Doctrine array collections should be used when the position of the items in the collection must be controlled. For instance, I have a Chart object which has various primitive properties as well as a series property which is an array collection. Each member in the series collection is a Serie object which has various primitive properties as well as a data property which is an array collection. Each member in the data collection is a Datum object which includes various primitive properties. The following just refers to the series collection, however, will be doing the same with the data collection. Chart will have methods to add a Serie to the end of the series collection, modify a Serie for a given position, delete a Serie for a given position, swap the position of two Serie, and retrieve all Serie(s) in their designated position order. To implement this, my thought is to include a integer position property in Serie, and when retrieving the results will just ORDER BY this column if using DQL or maybe will use Criteria to filter the results. I am not, however, sure how to implement some of the other methods. When adding a new Serie to the end of the collection, should I do something like: class Chart { public function addSerie(array $params):Chart { $serie=new Serie(); $serie->setName($params['name']); $position=$this->series->count(); $serie->setPosition($position); $this->addSeries($serie); } } Also, ArrayCollection has various methods such as offsetGet(), but how does the collection know what to be ordered by? Maybe using something like the following? class Chart { public function __construct() { $this->series->getIterator()->uasort(function ($first, $second) { return (int) $first->getPosition() > (int) $second->getPosition() ? 1 : -1; }); } } Also, any general thoughts on this topic would be appreciated. PS. Originally, I was using the name "Series" instead of "Serie", but the lack of separate forms for singular and plural was messing with me. Was thinking of using SeriesNode instead, but didn't want to need to follow this same naming convention for other items... Quote Link to comment Share on other sites More sharing options...
kicken Posted April 9, 2019 Share Posted April 9, 2019 When you define your entity you can specify what order you want the association to use when it's fetched. This will handle sorting it when querying the database initially. As far as saving the order as you manipulate it, you could just have a method on your chart that will update the position property of the collection items based on their position in the collection prior to being saved. You might be able to do this via a life cycle callback or just call it manually whenever you add/remove an item. class Chart { public function updateSeriesOrder(){ foreach ($this->series as $position=>$item){ $item->setPosition($position); } } } Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted April 9, 2019 Author Share Posted April 9, 2019 Thanks kicken, Looks like I was making a bigger issue than it needs to be. Let me just do it and make sure everything works as desired. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.