Question

I have two doctrine entities 'User' and 'Attribute', shown below. I need to construct a query that will retrieve all users and order them by attribute name where attribute type = x. For example, get all users and order them by 'title'.

SELECT u FROM User u JOIN u.attributes a ORDER BY a.name {something??} a.type = 'title'


class User {

    /**
     * @ManyToMany (targetEntity="Attribute", inversedBy="users", cascade={"persist"})
     * 
     */
    private $attributes;
}



class Attribute {

    /**
     * @Column (type="string", length=255, unique=false, nullable=false, name="name")
     * @FormElement (type="text")
     * @type string
     */
    protected $name;

    /**
     * @Column (type="string", unique=false, nullable=true, name="type")
     * @type string
     */
    private $type;

    /**
     * @Column (type="integer", length=11, unique=false, nullable=true, name="priority")
     * @type integer
     */
    private $priority;

    /**
     * @ManyToMany (targetEntity="User", mappedBy="attributes")
     */
    private $users;

}
Was it helpful?

Solution

I think this query is what you are looking for:

SELECT u FROM User u JOIN u.attributes a WHERE a.type = 'title' ORDER BY a.name ASC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top