找不到 ID id(155) 的类型为“AppBundleEntityUser"的实体

2022-01-16 00:00:00 php symfony doctrine

当我遍历一个组的用户(组#users,它是多对一关系)以显示用户电子邮件时,我收到此错误 Entity of type 'AppBundleEntityUser' for IDs id(155)找不到.但是,当我显示引发异常的用户时,我会看到:

When I iterate over users of one group (group#users and it's a ManyToOne relationship) to display the users emails, I get this error Entity of type 'AppBundleEntityUser' for IDs id(155) was not found. However, when I display the user for which the exception is raised I see this:

// In my controller
dump($groups[0]->getUser());

// Output
User {#761 ▼
  +__isInitialized__: false
  #id: 155
  #email: null
  #firstName: null
  #lastName: null
  #roles: null
   …2
}

此外,此用户(其 id 等于 155)确实存在于我的数据库中.

Besides, this user (whose id equals 155) does exist in my database.

更新 1

public function getByCriteria(array $criteria)
{
    $qb = $this->createQueryBuilder('g');
    // ....


    return $qb
            ->getQuery()
            // This does NOT change anything ?? Isn't this supposed to load related user??
            // http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql
            ->setFetchMode('class_namespace', 'user', ClassMetadata::FETCH_EAGER)
            ->getResult();
}

推荐答案

想通了!!实际上有一个启用的学说过滤器在每个 sql 查询中添加一个条件.当我得到一个组时,与其相关的用户不会从数据库中加载,只有它的 id 与组实体一起加载.当我尝试访问 id 以外的其他用户字段时,它会执行 sql join 查询.同样,学说过滤器将条件添加到连接条件中,这会导致结果为空.

Figured it out!! Actually there is a an enabled doctrine filter that adds one condition in every sql query. When I get one group, the user being related to it is not loaded from the database, only its id is loaded with the group entity. When I try to access other user field than the id, it performs the sql join query. Again, the doctrine filter adds the condition even to the join condition, which leads to an empty result.

所以错误信息显然具有误导性.

So the error message is clearly misleading.

相关文章