Entity of type 'AppBundle\Entity\User' for

2019-06-28 05:27发布

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 'AppBundle\Entity\User' 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
}

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

UPDATE 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();
}

2条回答
爷、活的狠高调
2楼-- · 2019-06-28 05:57

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.

查看更多
别忘想泡老子
3楼-- · 2019-06-28 06:00

I identified that there were orphaned children in the table in question, for some reason, removing an object was not cascaded.

In my case:

SELECT * FROM ACCESS GROUP WHERE ID = 2; 
-- 0 RESULTS.

SELECT * FROM GROUP_ACESSO_USUARIO_CONTA WHERE fk_grupo_acesso = 2; 
-- 2 RESULTS.
查看更多
登录 后发表回答