Symfony fetchAll通过过滤器

Symfony fetchAll通过过滤器,symfony,Symfony,我想从用户记录中筛选输出。我将筛选活动、非活动或具有指定角色或分配给某个地区的用户 我在我的用户存储库中创建了一个函数 public function getListBy($filter) { $qb = $this->createQueryBuilder('i'); $associateMapping = $this->getClassMetadata()->getAssociationMappings(); foreach ($filter as

我想从用户记录中筛选输出。我将筛选活动、非活动或具有指定角色或分配给某个地区的用户

我在我的用户存储库中创建了一个函数

public function getListBy($filter)
{
    $qb = $this->createQueryBuilder('i');
    $associateMapping = $this->getClassMetadata()->getAssociationMappings();

    foreach ($filter as $field => $value) {
         if (array_key_exists($field, $associateMapping)) {
            switch ($field) {
                case 'roles':
                    $qb->innerJoin('i.'.$field, 'r');
                    $qb->andWhere('r.id=:id')->setParameter('id', $value->getId());
                break;
                case 'wards':
                    $qb->innerJoin('i.'.$field, 'd');
                    $qb->andWhere('d.id=:id')->setParameter('id', $value->getId());
                break;
         }
    }

    return $qb->getQuery()->getResult();
}
如果我为指定的地区或仅为指定的角色筛选onyl,这是可行的。该函数返回受影响的用户

但是,如果我将过滤器组合在一起,并过滤指定地区中具有指定角色的用户,则不会得到任何结果

我的表格结构的简单示例:

用户:

角色:

地区:

+----+------+
| id | name |
+----+------+
|  1 | ny   |
+----+------+
关系表:

用户角色:

+---------+---------+
| role_id | user_id |
+---------+---------+
|       1 |       1 |
+---------+---------+
地区对用户

+-------------+---------+
| district_id | user_id |
+-------------+---------+
|           1 |       1 |
+-------------+---------+
有没有人建议我做错了什么

更新

我收到的sql响应如下所示

选择u0.id作为id0,u0.firstName作为firstName1,u0.lastName作为lastName2,u0.username作为username3,u0.password4,u0.is作为password,u0.is作为activity,u0.createDate作为createDate6,u0.gender作为gender7,u0.title作为title8,u0.modifiedDate作为modifiedDate9,u0.lastLogin作为ModifiedData10,u0.maritalStatus作为MariTal11,儿童12,出生日期13,出生地点14,国籍15,u0_uu.id.newUser作为newUser16从u0_u上的用户u0_uu内部加入用户u角色u2_u.id=u2.id内部加入角色r1_u在r1上。id=u2_0.id内部加入用户u地区u4_0.id=u4_4.id用户u id内部加入地区d3_3在d3上。id=u4_4地区u id在哪里4.id=?和d3_.id=

如果我在数据库上执行这个查询,它将返回一个结果。但在应用程序中,我没有得到结果

$qb->getQuery()->getResult() 
更新2

我绝望了。现在,我删除了我的存储库函数中的任何内容,只写以下几行

public function getListBy($criteria)
{
    $qb = $this->createQueryBuilder('i');

    $qb->innerJoin('i.roles', 'roles');
    $qb->andWhere('roles.id=:id')->setParameter('id', 9);

    $qb->innerJoin('i.wards', 'wards');
    $qb->andWhere('wards.id=:id')->setParameter('id', 1);

    // If i execute this output in my database, i get one row as result
    echo $qb->getQuery()->getSql();

    // But if i count the result, it return 0
    var_dump(count($qb->getQuery()->getResult()));

    return $qb->getQuery()->getResult();
 }
不幸的是,我没有更多的想法是非常奇怪的。有没有人有过这个问题

更新3,我解决我的问题:

我的错误在函数“setParameter”中。在这两种情况下,我在switch语句中使用相同的标识符来替换参数。在我不使用设置参数后,一切正常

public function getListBy($criteria)
{
    $qb = $this->createQueryBuilder('i');

    $qb->innerJoin('i.roles', 'roles');
    $qb->andWhere('roles.id=9');

    $qb->innerJoin('i.wards', 'wards');
    $qb->andWhere('wards.id=1');

    // If i execute this output in my database, i get one row as result
    echo $qb->getQuery()->getSql();

    // But if i count the result, it return 0
    var_dump(count($qb->getQuery()->getResult()));

    return $qb->getQuery()->getResult();
 }

但我不明白为什么标识符的名称相同是一个问题。有人能给我解释一下这种情况吗?

试试echo$qb->getQuery()->getSQL();要输出CREATESQL查询并在db上运行它以查看查询是否正确创建,我尝试这样做,但看不到完整的sql语句。在757个字符之后,输出仅显示“…长度=757”try echo$qb->getQuery()->getSQL();死亡所以执行在echoWith echo之后终止,现在我看到了完整的查询。在我尝试var_dump之前,对不起,我的错误查询看起来像您预期的那样吗?
public function getListBy($criteria)
{
    $qb = $this->createQueryBuilder('i');

    $qb->innerJoin('i.roles', 'roles');
    $qb->andWhere('roles.id=:id')->setParameter('id', 9);

    $qb->innerJoin('i.wards', 'wards');
    $qb->andWhere('wards.id=:id')->setParameter('id', 1);

    // If i execute this output in my database, i get one row as result
    echo $qb->getQuery()->getSql();

    // But if i count the result, it return 0
    var_dump(count($qb->getQuery()->getResult()));

    return $qb->getQuery()->getResult();
 }
public function getListBy($criteria)
{
    $qb = $this->createQueryBuilder('i');

    $qb->innerJoin('i.roles', 'roles');
    $qb->andWhere('roles.id=9');

    $qb->innerJoin('i.wards', 'wards');
    $qb->andWhere('wards.id=1');

    // If i execute this output in my database, i get one row as result
    echo $qb->getQuery()->getSql();

    // But if i count the result, it return 0
    var_dump(count($qb->getQuery()->getResult()));

    return $qb->getQuery()->getResult();
 }