【问题标题】:doctrine 2 order by associative tabel教义2按关联表排序
【发布时间】:2010-11-17 02:31:47
【问题描述】:

我有两个学说实体“用户”和“属性”,如下所示。我需要构建一个查询,该查询将检索所有用户并按属性名称对它们进行排序,其中属性类型 = x。例如,获取所有用户并按“标题”排序。

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;

}

【问题讨论】:

    标签: sql-order-by dql doctrine-orm


    【解决方案1】:

    我认为这个查询就是你要找的:

    SELECT u FROM User u JOIN u.attributes a WHERE a.type = 'title' ORDER BY a.name ASC
    

    【讨论】:

    • 这不会返回所有用户。它只会返回那些具有“title”类型属性的用户。假设您有三个用户:john、ringo、paul。 John 有一个属性,类型为“title”,名称为“writer”,ringo:类型为“title”,名称为“drummer”,而 paul 没有标题。现在,结果显示依次为 ringo(drummer)、john(writer)、paul(null)
    • 嗨 waigani,您尝试左连接了吗? SELECT u FROM User u LEFT JOIN u.attributes a WITH a.type = 'title' ORDER BY a.name ASC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多