在链配置的命名空间 AppBundle\Document 中找不到 class 'Doctrine\ODM\MongoDB\Cursor'

The class 'Doctrine\ODM\MongoDB\Cursor' was not found in the chain configured namespaces AppBundle\Document

在存储库中我有这个代码:

<?php

namespace AppBundle\Repository;

use Doctrine\ODM\MongoDB\DocumentRepository;

class ItemRepository extends DocumentRepository
{
    public function findAllQueryBuilder($filter = '')
    {
        $qb = $this->createQueryBuilder('item');

        if ($filter) {
            $cat = $this->getDocumentManager()
                ->getRepository('AppBundle:Category')
                ->findAllQueryBuilder($filter)->getQuery()->execute();


            $qb->field('category')->includesReferenceTo($cat);
        }

        return $qb;
    }
}

但是它抛出这个错误:

The class 'Doctrine\ODM\MongoDB\Cursor' was not found in the chain configured namespaces AppBundle\Document 

有什么问题?

我检查了 $cat,它 returns 正确 category 文档。

$cat变量是Doctrine\ODM\MongoDB\Cursor的实例。但它应该是文档的实例。 所以代码应该改为:

<?php

namespace AppBundle\Repository;

use Doctrine\ODM\MongoDB\DocumentRepository;

class ItemRepository extends DocumentRepository
{
    public function findAllQueryBuilder($filter = '')
    {
        $qb = $this->createQueryBuilder('item');

        if ($filter) {
            $cats = $this->getDocumentManager()
                ->getRepository('AppBundle:Category')
                ->findAllQueryBuilder($filter)->getQuery()->execute();

            foreach ($cats as $cat) {
                $qb->field('category')->includesReferenceTo($cat);
            }
        }

        return $qb;
    }
}