Entity of type 'AppBundle\Entity\User' for IDs id(155) was not found

ⅰ亾dé卋堺 提交于 2019-12-23 13:22:59

问题


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

回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/40615232/entity-of-type-appbundle-entity-user-for-ids-id155-was-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!