Symfony 4 + Sonata + Sonata Doctrine ORM Admin Bundle: Error: No Metadata classes to process

Symfony 4 + Sonata + Sonata Doctrine ORM Admin Bundle: Error: No Metadata classes to process

我正在尝试使用 Sonata Doctrine ORM Admin Bundle 让 Sonata 与 Symfony 4 一起工作。

我已经安装了以下内容(不确定是否所有这些都是必需的)并将我的数据库详细信息添加到 .env 文件中,这确实显示了一个空白的奏鸣曲管理页面。

symfony-skeleton
sonata-project/admin-bundle
sonata-project/doctrine-orm-admin-bundle
symfony/orm-pack
symfony annotations

现在我想将实体添加到我的项目中,所以我从教程中复制了一些实体,将它们放在 src\Entity 文件夹中并添加了 namespaceuse as ORM 陈述:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

// ...
use Doctrine\Common\Collections\ArrayCollection;
// ...

class Category
{
    // ...

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string")
     */
    private $name;

    /**
    * @ORM\OneToMany(targetEntity="BlogPost", mappedBy="category")
    */
    private $blogPosts;

    public function __construct()
    {
        $this->blogPosts = new ArrayCollection();
    }

    public function getBlogPosts()
    {
        return $this->blogPosts;
    }

    // ...
}

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

// ...
class BlogPost
{
    // ...

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string")
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="body", type="text")
     */
    private $body;

    /**
     * @var bool
     *
     * @ORM\Column(name="draft", type="boolean")
     */
    private $draft = false;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="blogPosts")
     */
    private $category;
}

但是当我 运行 php bin/console doctrine:schema:create 它告诉我 No Metadata classes to process.

我错过了什么?

app\config\packages\doctrine.yaml:

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

app\config\bundles.php

<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
    Sonata\DatagridBundle\SonataDatagridBundle::class => ['all' => true],
    Sonata\CoreBundle\SonataCoreBundle::class => ['all' => true],
    Sonata\BlockBundle\SonataBlockBundle::class => ['all' => true],
    Knp\Bundle\MenuBundle\KnpMenuBundle::class => ['all' => true],
    Sonata\AdminBundle\SonataAdminBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
];

这可能是由于清理缓存造成的,因为 Doctrine 正在缓存所有数据。试试这个命令:

php bin/console cache:clear --env=prod
php bin/console cache:clear --env=dev

您的 app/config/config.yml 文件中应该有此配置:

doctrine:

    ...

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            AppBundle:
                mapping: true
                type: annotation
                alias: Blog
                prefix: App\Bundle\Entity

并确保您的 Sonata AdminBundle 已在 AppKernel 中注册。

在这里找到了我的问题的答案:

我使用的教程中的示例 类 没有 @Entity 注释,因此被 Doctrine 跳过。