Symfony2:Left Join产生错误的查询

时间:2015-01-16 01:12:53

标签: php mysql symfony doctrine-orm

我正在尝试使用doctrine查询构建器构建一个查询,如下所示:

$q = $this
        ->createQueryBuilder('u')
        ->select('u, r')
        ->leftJoin('u.roles', 'r')
        ->where('u.username = :username OR u.email = :email')
        ->setParameter('username', $username)
        ->setParameter('email', $username)
        ->getQuery();`

此代码生成2个查询:

查询1是正确的

SELECT t0.user_id AS user_id1, t0.username AS username2, t0.salt AS salt3, t0.password AS password4, t0.email AS email5, t0.is_active AS is_active6, t0.created AS created7, t0.updated AS updated8, t0.last_login AS last_login9 
FROM users t0 
WHERE t0.username = ? 
LIMIT 1 

查询2不正确:

SELECT t0.role_id AS role_id1, t0.role AS role2 
FROM roles t0 
INNER JOIN user_role ON t0.id = user_role.role_id 
WHERE user_role.user_fk = ?

查询2应为:

SELECT t0.role_id AS role_id1, t0.role AS role2 
FROM roles t0 
INNER JOIN user_role ON t0.role_id = user_role.role_fk 
WHERE user_role.user_fk = ?

实体\角色如下所示:

/**
 * @ORM\Table(name="roles")
 * @ORM\Entity(repositoryClass="XXX\XXXBundle\Entity\Repository\RoleRepository")
 */
class Role implements RoleInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(name="role_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $_roleId;
    /**
     * @ORM\Column(name="role", type="string", length=20, unique=true)
     */
    protected $_role;
    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
     * @ORM\JoinTable(name="user_role",
     *     joinColumns={@ORM\JoinColumn(name="role_fk", referencedColumnName="role_id")})
     */
    private $_users;
...

实体\用户看起来像这样:

class User implements UserInterface, \Serializable  
    {
        /**
         * @ORM\Column(name="user_id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $_userId;
        ...
        /**
         * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
         * @ORM\JoinTable(name="user_role",
         *     joinColumns={@ORM\JoinColumn(name="user_fk", referencedColumnName="user_id")})
         */
        protected $_roles;
    ...

Entity \ Role代码中的注释指出要使用的列名称,并且语句的SELECT部分​​使用正确的名称。该语句的WHERE部分使用正确的列user_role.user_fk,这是在Entity \ User代码中定义的。

如何使用不存在的列名停止教义,并使用已定义的列名称作为语句的INNER JOIN部分?

1 个答案:

答案 0 :(得分:1)

我找到了一个有效的解决方案。

实体\用户需要更改为在注释中包含inverseJoinColumns,如下所示:

/**
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="_users")
 * @ORM\JoinTable(name="user_role",
 *     joinColumns={@ORM\JoinColumn(name="user_fk", referencedColumnName="user_id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_fk", referencedColumnName="role_id")})
 */
protected $_roles;

实体\角色需要更改为包含inverseJoinColumns,如下所示:

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="_roles")
 * @ORM\JoinTable(name="user_role",
 *     joinColumns={@ORM\JoinColumn(name="role_fk", referencedColumnName="role_id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="user_fk", referencedColumnName="user_id")})
 */
private $_users;

并且createQueryBuilder代码需要如下所示:

$q = $this
        ->createQueryBuilder('u')
        ->select('u, r')
        ->from('XXXXXXBundle:User', 'u')
        ->leftJoin('u.roles', 'r', \Doctrine\ORM\Query\Expr\Join::ON, 'user_role.role_kf = r.role_id')
        ->where('u.username = :username OR u.email = :email')
        ->setParameter('username', $username)
        ->setParameter('email', $username)
        ->getQuery();