[DCOM-292] orderBy(array("field1"=>Criteria::X, "field2"=>Criteria::Y)) Only works for field1 Created: 31/Jul/15  Updated: 31/Jul/15

Status: Open
Project: Doctrine Common
Component/s: Collections
Affects Version/s: 2.5.0
Fix Version/s: None
Security Level: All

Type: Bug Priority: Major
Reporter: Diego Cebrian Assignee: Benjamin Eberlei
Resolution: Unresolved Votes: 0
Labels: collection
Environment:

Apache/2.4.12 (Ubuntu)
PHP Version 5.6.11-1+deb.sury.org~trusty+1



 Description   

When trying to filter one collection (Doctrine\Common\Collections\ArrayCollection) which is generated as result of a OneToMany Relation only works the first orderBy, but not the others:

PossibleDate.php
// Some comments here
/**
 * Event
 *
 * @ORM\Table(name="agenda__date_possible")
 * @ORM\Entity(repositoryClass="App\AgendaBundle\Entity\PossibleDateRepository")
 */
class PossibleDate
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="startDate", type="datetime")
     */
    private $startDate;

    /**
     *
     * @var collection
     *
     * @ORM\ManyToOne(targetEntity="App\AgendaBundle\Entity\ProposedEvent", inversedBy="possibleDates")
     * @ORM\JoinColumn(name="proposed_event_id", nullable=false)
     **/
    protected $proposedEvent;

    /**
     * @var integer
     *
     * @ORM\Column(name="number_of_votes", type="integer")
     */
    private $numberOfVotes;

    /**
     * Set startDate
     *
     * @param \DateTime $startDate
     * @return Event
     */
    public function setStartDate($startDate)
    {
        $this->startDate = $startDate;

        return $this;
    }

    /**
     * Get startDate
     *
     * @return \DateTime 
     */
    public function getStartDate()
    {
        return $this->startDate;
    }

    /**
     * Set votes
     *
     * @param integer $votes
     * @return Event
     */
    public function setNumberOfVotes($numberOfVotes)
    {
        $this->numberOfVotes = $numberOfVotes;

        return $this;
    }

    /**
     * Get votes
     *
     * @return integer 
     */
    public function getNumberOfVotes()
    {
        return $this->numberOfVotes;
    }
ProposedEvent.php
// Some comments here
/**
 * Event
 *
 * @ORM\Table(name="agenda__event")
 * @ORM\Entity(repositoryClass="App\AgendaBundle\Entity\ProposedEventRepository")
 */
class ProposedEvent
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     *
     * @var collection
     *
     * @ORM\OneToMany(targetEntity="App\AgendaBundle\Entity\PossibleDate", mappedBy="proposedEvent", cascade={"persist"})
     **/
    protected $possibleDates;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->possibleDates = new ArrayCollection();
    }


    /**
     * Add possibleDate
     *
     * @param \App\AgendaBundle\Entity\PossibleDate $possibleDate
     * @return ProposedEvent
     */
    public function addPossibleDate(\App\AgendaBundle\Entity\PossibleDate $possibleDate)
    {
        $this->possibleDates[] = $possibleDate;
        $possibleDate->setProposedEvent($this);

        return $this;
    }

    /**
     * Remove possibleDate
     *
     * @param \App\AgendaBundle\Entity\PossibleDate $possibleDate
     */
    public function removePossibleDate(\App\AgendaBundle\Entity\PossibleDate $possibleDate)
    {
        $this->possibleDates->removeElement($possibleDate);
    }

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

    public function getSelectedDate()
    {
        //TODO: Correct Date (Second ordering criteria not working)
        $criteria = Criteria::create()
            ->where(Criteria::expr()->gt("startDate", new \DateTime()))
            ->orderBy(array("numberOfVotes" => Criteria::DESC, "startDate"=>Criteria::ASC))
            ->setFirstResult(0)
            ->setMaxResults(1);

        $selectedDates = $this->getPossibleDates()->matching($criteria);
        return $selectedDates[0];
    }

When I use the getSelectedDate() in a environment with more than one PossibleDate with the max numberOfVotes possible, the function returns a random of the PossibleDates wich have the maximum number of votes, but not taking in account the startDate ordering criteria.

There is no query to database because the getSelectedDate() is called with the array collection loaded.






Generated at Tue Aug 04 11:50:29 UTC 2015 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.