Symfony 4 make:entity --regenerate 失败:在“App”命名空间中找不到实体

问题描述 投票:0回答:6

来自composer.json

{
    "name": "symfony/website-skeleton",
    "type": "project",
    "license": "MIT",
    "description": "A skeleton to start a new Symfony website",
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "sensio/framework-extra-bundle": "^5.1",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "^4.0",
        "symfony/console": "^4.0",
        "symfony/expression-language": "^4.0",
        "symfony/flex": "^1.0",
        "symfony/form": "^4.0",
        "symfony/framework-bundle": "^4.0",
        "symfony/lts": "^4@dev",
        "symfony/monolog-bundle": "^3.1",
        "symfony/orm-pack": "^1.0",
        "symfony/process": "^4.0",
        "symfony/security-bundle": "^4.0",
        "symfony/serializer-pack": "*",
        "symfony/swiftmailer-bundle": "^3.1",
        "symfony/translation": "^4.0",
        "symfony/twig-bundle": "^4.0",
        "symfony/validator": "^4.0",
        "symfony/web-link": "^4.0",
        "symfony/webpack-encore-pack": "*",
        "symfony/yaml": "^4.0"
    },
    "require-dev": {
        "symfony/browser-kit": "^4.0",
        "symfony/css-selector": "^4.0",
        "symfony/debug-pack": "*",
        "symfony/dotenv": "^4.0",
        "symfony/maker-bundle": "^1.5",
        "symfony/phpunit-bridge": "^4.0",
        "symfony/profiler-pack": "*",
        "symfony/web-server-bundle": "^4.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false
        }
    }
}

来自doctrine.yaml:

# /config/packages/doctrine.yaml
doctrine:
    dbal:
        default_connection: training
        connections:
            training:
                dbname:   "training"
                driver:   "pdo_mysql"
                host:     "localhost"
                port:     "3306"
                user:     "userdb"
                password: "XXXXXXXXXX"
                charset:  UTF8
                mapping_types:
                    bit: boolean
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: training
        entity_managers:
            training:
                connection: training
                auto_mapping: true

命令:

php bin/console doctrine:mapping:import --em=training App\Entity annotation --path=src/Entity

生成

src/Entity
Course.php
实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Course
 *
 * @ORM\Table(name="course", indexes={@ORM\Index(name="id_product_idx_1", columns={"id_product"}), @ORM\Index(name="enabled_idx_1", columns={"enabled"}), @ORM\Index(name="sort_idx_1", columns={"sort"})})
 * @ORM\Entity
 */
class Course
{
    ....

运行到控制台后:

php bin/console make:entity --regenerate App

此操作失败并出现错误:

在“App”命名空间中找不到实体

同样

php bin/console make:entity --regenerate App\Entity 

如果我尝试

php bin/console make:entity --regenerate App\Entity\Course

输出是

找不到“App\Entity\Course”的 Doctrine 元数据。它是否映射为实体?

我做错了什么?

php symfony doctrine symfony4
6个回答
4
投票

就我而言,这是因为我写道:

php bin/console make:entity --regenerate App\Entity\Course 

而不是(注意双反斜杠)

php bin/console make:entity --regenerate App\\Entity\\Course


1
投票

config/packages/doctrine.yaml

 dir: '%kernel.project_dir%/src/Entity'

这就是问题所在。每个 .orm.yml 都在该路径中搜索。


1
投票

我有同样的错误,我找到的解决方法是将repositoryClass添加到我的所有实体中:

@ORM\Entity(repositoryClass="App\Repository\DefaultRepository")

最终代码是这样的:

/**
 * Key
 *
 * @ORM\Table(name="`key`")
 * @ORM\Entity(repositoryClass="App\Repository\DefaultRepository")
 */

希望对你有帮助


1
投票

内部

app/config/packages/doctrine.yaml

...
doctrine:

...

 orm:
        default_entity_manager: default
        auto_generate_proxy_classes: '%kernel.debug%'
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: default


                #ADD THESE LINES AND CHANGE PATH IF NEEDED
                mappings:
                    App:
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/'
                        prefix: 'App\Entity\'

奔跑

php bin/console make:entity --regenerate


1
投票

在实体类中,确保命名空间写得很好,如下所示:

namespace App\Entity


0
投票

本说明适用于 Symfony 6;

Symfony 6 使用属性而不是注释。由主义重新生成的实体:导入命令生成带有注释的实体。由于这个“php bin/console make:entity --regenerate "App\Entity"”不起作用。首先,必须使用 ctor.php 来迁移它,然后“php bin/console make:entity --regenerate "App\Entity" 起作用;

校长示例;

declare(strict_types=1);

use Rector\Symfony\Set\SymfonySetList;
use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src/Entity'
    ]);
    $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml');
    $rectorConfig->sets([
        SymfonySetList::SYMFONY_60,
        SymfonySetList::SYMFONY_CODE_QUALITY,
        SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION
    ]);

    $rectorConfig->sets([
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
        //SensiolabsSetList::FRAMEWORK_EXTRA_61

    ]);


};

© www.soinside.com 2019 - 2024. All rights reserved.