Symfony + Doctrine ODM "The class was not found in the chain configured namespaces CoreBundle/Document"

Symfony + Doctrine ODM "The class was not found in the chain configured namespaces CoreBundle/Document"

我找不到导致此问题的原因。我有一个名为 CoreBundle (src/CoreBundle) 的包,其中有一个 Entity 目录 (src/CoreBundle/Entity)。该目录存储所有实体。目前只有用户实体 (src/CoreBundle/Entity/User.php)。内容如下:

namespace Mokapot\CoreBundle\Entity;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @MongoDB\Document(collection="users")
 */
class User implements UserInterface {

    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $username;

    /**
     * @MongoDB\String
     */
    protected $password;

    /**
     * @MondoDB\Collection
     */
    private $roles;

    public function getRoles() {
        $roles = $this->roles;
        if(empty($roles)) {
            $roles = ['ROLE_USER'];
        }
        return array_unique($roles);
    }

    public function setRoles(array $roles) {
        $this->roles = $roles;
    }

    public function getSalt() {
        return;
    }

    public function eraseCredentials() {

    }

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setUsername($username) {
        $this->username = $username;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

}

这是我的 config.yml 学说:

doctrine_mongodb:
    connections:
        default:
            server: "%dbhost%"
            options: {}
    document_managers:
        default:
            database: "%dbname%"
            auto_mapping: false
            mappings:
                CoreBundle:
                    mapping: true
                    type: annotation
                    dir: Entity
                    prefix:               ~
                    alias:                ~
                    is_bundle: true

编辑:更改了标题。

尝试像这样更改您的命名空间:

namespace Mokapot\CoreBundle\Document;

和目录:

src/CoreBundle/Document

官方文档:

Suppose you're building an application where products need to be displayed. Without even thinking about Doctrine or MongoDB, you already know that you need a Product object to represent those products. Create this class inside the Document directory of your AcmeStoreBundle:

我找到了解决办法。我将 prefix 变量更改为

prefix: CoreBundle\Entity