Symfony:内部连接查询的相反

时间:2015-05-18 21:00:07

标签: php mysql symfony

使用Symfony的createQueryBuilder寻求复制此SQL查询:

Select * from user
left join location on location.id = user.id
where location.id is null

尝试过:

$er->createQueryBuilder('u')
    ->leftJoin('u.location','l')
    ->where('l.id = :id ')
    ->setParameter('id',null);

实体 - 用户,位置。用户与地点有OneToMany关系。

QueryBuilder生成SQL

SELECT u0_.id AS id0, u0_.firstname AS firstname1, u0_.lastname AS lastname2, u0_.creationDate AS creationDate3 
FROM User u0_ 
LEFT JOIN Locations l1_ ON u0_.id = l1_.user_id 
WHERE l1_.id = ? 
ORDER BY u0_.firstname AS

1 个答案:

答案 0 :(得分:1)

找到解决方案。如果id为空,则更改$qBuilder = $er->createQueryBuilder('u') ->leftJoin('u.locations','l') ->where('l.id is NULL'); 的检查方式。

using (var mock = AutoMock.GetLoose())
{
            const string mainUserID = "MainUserID";
            const string otherUserID = "OtherUserID";
            ApplicationUser user = new ApplicationUser()
            {
                Id = mainUserID,
                UserName = "TestUser"
            };

            var dataProvider = mock.Mock<IDataProtectionProvider>();
            dataProvider.DefaultValue = DefaultValue.Mock;

            var userManagerMock = mock.Mock<ApplicationUserManager>();
}

希望它会对某人有所帮助。