2 doctrina fin de tabla asociativa
-
25-09-2019 - |
Pregunta
Tengo dos entidades doctrina 'Usuario' y 'atributo', que se muestra a continuación. Necesito construir una consulta que va a recuperar todos los usuarios y les ordenará por nombre de atributo, donde x = tipo de atributo. Por ejemplo, obtener todos los usuarios y ordenarlas por 'título'.
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;
}
Solución
Creo que esta consulta es lo que busca:
SELECT u FROM User u JOIN u.attributes a WHERE a.type = 'title' ORDER BY a.name ASC
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow