5

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();
}
smarber
  • 4,829
  • 7
  • 37
  • 78
  • how are your group->user associations loaded? seems like lazy loading user is not active. I recommend a custom repository fetch join for performance anyway. – LBA Nov 15 '16 at 17:20

2 Answers2

5

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.

smarber
  • 4,829
  • 7
  • 37
  • 78
  • And how did you fix it? – chi Apr 19 '17 at 10:12
  • Several solutions: ((1- to remove the filter editing doctrine queries on the fly)) ((2- to perform native queries, which won't be edited by doctrine filters)) – smarber Apr 19 '17 at 12:39
  • By filter editing doctrine queries are you reffering for example to find or findBy methods from EntityRepo? Because I'm using them and I get the error. I don't what to change my methods and I think that might be another solutions :-? – chi Apr 19 '17 at 13:36
  • Thank you man, this problem was driving me crazy! – lorenzo Oct 24 '22 at 16:00
0

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.
barbsan
  • 3,418
  • 11
  • 21
  • 28