Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:29:32+00:00 2026-05-23T02:29:32+00:00

I want to sort my model’s associated ArrayCollection with a quotient, like this (I

  • 0

I want to sort my model’s associated ArrayCollection with a quotient, like this (I know the following code doesn’t work):

/** 
 * @OneToMany (targetEntity="Review", mappedBy="product") 
 * @OrderBy ({"voted_up / voted_down" = "DESC"})
 */
protected $reviews;

Is something this possible directly in the model definition or do I need to simply use a sort() on the ArrayCollection when requesting the data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:29:32+00:00Added an answer on May 23, 2026 at 2:29 am

    With Doctrine 2.1 you can do that directly in the model definition, but not with @OrderBy. You can define DQL snippets at model level, like stated in the 2.1 Beta release notes:

    Named DQL Queries in Metadata: You can add dql queries in the mapping files using @NamedQueries(@NamedQuery(name=”foo”, query=”DQL”)) and access them through $em->getRepository()->getNamedQuery().

    As such you can create your DQL query with the ORDER BY keywords, something like:

    SELECT c.id, c.text, (c.voted_up / c.voted_down) AS sortkey FROM Comment c
    ORDER BY sortkey DESC
    

    So, I imagine you add this annotation to the model definition, something like:

    /**
     * @Entity
     * @Table(name="comment")
     * @NamedQueries(@NamedQuery(name="sortedComment", query="SELECT c.id, c.text, (c.voted_up / c.voted_down) AS sortkey FROM Comment c ORDER BY sortkey DESC"))
     */
     class Comment {
         ...
     }
    

    And then on your code call:

    $em->getRepository("Comment")->getNamedQuery("sortedComment");
    

    I didn’t test this, but you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add few fields to every model in my django application. This
I want to create the following relationship. Obviously this is painfully basic but I'm
I want do fit some sort of multi-variate time series model using R. Here
I want to model an article with revisions in Django: I have following in
My model looks like this (example): class Bank < ActiveRecord::Base has_and_belongs_to_many :currencies # other
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have a Post model that has_one Status. I want to sort the objects
I have a model of type QStandardItemModel which looks like this: QHash<int, QByteArray> roleNames;
I want my Data Model and my Getters and Setters set like a simple
I am using the following ASM routine to bubble sort an array. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.