Symfony2 - Note: Undefined offset: 0 due to user request

I have this error:

"Note: offset Undefined: 0 in C: \ WAMP \ WWW \ video library \ provider \ doctrine \ Lib \ Doctrine \ ORM \ QueryBuilder.php line 240"

I am creating a video collection on the Internet. There are 2 objects: film and genre. In my GenRerepository method, I tried to override the findAll () function to the number of movies associated with the genre.

This is the function:

public function myFindAll() { $genres = $this->_em->createQueryBuilder('g') // leftJoin because I need all the genre ->leftJoin('g.films', 'f') ->addSelect('COUNT(f)') ->groupBy('g') ->getQuery() ->getArrayResult(); // $genres contains all the genres and the associated movies return ($genres); } 
+4
source share
3 answers

The Repository class provides a method for creating a QueryBuilder already configured for an object, so we can directly supply:

 $this->createQueryBuilder('g') 
+6
source

Try the following:

 public function myFindAll() { $qb = $this->_em->createQueryBuilder('g'); $qb->leftJoin('g.films', 'f') ->select($qb->expr()->count('f')) // Use an expression ->groupBy('g.id') // Specify the field ; $genres = $qb->getQuery()->getArrayResult(); // $genres contains all the genres and the associated movies return ($genres); } 
0
source

I found this problem myself and wanted to publish my solution. Since you are creating a queryBuilder outside of an EntityManager instead of an EntityRepository, you need a from statement. The error occurs when there is no from statement or if the from statement does not work (from what needs to be done to join it so that it is happy). EntityRepository will take care of this for you when using it.

 public function myFindAll() { $genres = $this->_em->createQueryBuilder('g') //from statement here ->from('GenreEntityClass', 'g') // leftJoin because I need all the genre ->leftJoin('g.films', 'f') ->addSelect('COUNT(f)') ->groupBy('g') ->getQuery() ->getArrayResult(); // $genres contains all the genres and the associated movies return ($genres); } 
0
source

All Articles