Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sort an entity's arrayCollection in symfony2

I have an entity "container" with this property

/**
 * @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
 */
private $content;

the property is an array collection...

public function __construct() {
    $this->content = new \Doctrine\Common\Collections\ArrayCollection();
}

...with these two standard methods

/**
 * Add content
 *
 * @param BizTV\ContentManagementBundle\Entity\Content $content
 */
public function addContent(\BizTV\ContentManagementBundle\Entity\Content $content)
{
    $this->content[] = $content;
}

/**
 * Get content
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getContent()
{
    return $this->content;
}

Now my question is, is there a smooth way to build a sorting feature into this, perhaps on the getContent() call? I am no php wiz and certainly not seasoned in symfony2 but I learn as I go.

The content entity itself has a sorting INT like this that I want to sort it on:

/**
 * @var integer $sortOrder
 *
 * @ORM\Column(name="sort_order", type="integer")
 */
private $sortOrder; 
like image 896
Matt Welander Avatar asked Jan 11 '13 15:01

Matt Welander


3 Answers

You should be able to use the @ORM\OrderBy statement which allows you to specify columns to order collections on:

/**
 * @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
 * @ORM\OrderBy({"sort_order" = "ASC"})
 */
private $content;

In fact this may be a duplicate of How to OrderBy on OneToMany/ManyToOne

Edit

Checking for implementation advice it appears that you must fetch the tables with a join query to the collection in order for the @ORM\OrderBy annotation to work: http://www.krueckeberg.org/notes/d2.html

This means that you must write a method in the repository to return the container with the contents table joined.

like image 124
Luke Avatar answered Sep 17 '22 13:09

Luke


If you want to be sure that you always get your relations in the order based on current property values, you can do something like this:

$sort = new Criteria(null, ['Order' => Criteria::ASC]);
return $this->yourCollectionProperty->matching($sort);

Use that for example if you've changed the Order property. Works great for a "Last modified date" as well.

like image 27
Wouter van Vliet Avatar answered Sep 17 '22 13:09

Wouter van Vliet


You can write

@ORM\OrderBy({"date" = "ASC", "time" = "ASC"})

for multiple criteria ordering.

like image 37
Yasmany Hernandez Hernandez Avatar answered Sep 18 '22 13:09

Yasmany Hernandez Hernandez