Jump to content

Doctrine ArrayCollection order


NotionCommotion

Recommended Posts

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...

Link to comment
Share on other sites

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);
        }
    }
}

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.