Symfony2 - 注意:由于自定义查询,未定义的偏移量为0

Adr*_*n G 4 php entity-relationship doctrine-orm

我有这个错误:

"注意:未定义的偏移量:0:C:\ wamp\www\Videotheque\vendor\doctrine\lib\Doctrine\ORM\QueryBuilder.php第240行"

我在线创建视频集.有两个实体:电影和流派.在我的GenRerepository方法中,我尝试将函数findAll()重新定义为与类型相关的电影数量.

这是功能:

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);
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*n G 6

Repository类提供了创建已为实体配置的QueryBuilder的方法,因此我们可以直接放置:

$this->createQueryBuilder('g')
Run Code Online (Sandbox Code Playgroud)


Flo*_*ent 0

尝试这个:

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);
}
Run Code Online (Sandbox Code Playgroud)