Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCOM-292: orderBy(array("field1"=>Criteria::X, "field2"=>Criteria::Y)) Only works for field1 #604

Closed
doctrinebot opened this issue Jul 31, 2015 · 2 comments
Assignees
Labels

Comments

@doctrinebot
Copy link

Jira issue originally created by user hisie:

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:

// 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;
    }
// 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.

@ostrolucky
Copy link
Member

Duplicate of #609, solved with doctrine/collections#64, can be closed

@Ocramius Ocramius assigned Ocramius and unassigned beberlei Jun 24, 2017
@Ocramius
Copy link
Member

Thanks @ostrolucky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants