phpsymfonysymfony6

Symfony make:entity fails


I have a problem creating entity on Symfony 6.4. When I try to symfony console make:entity it fails because:

C:\Users\pc\Desktop\Academy-ERP>php bin/console make:entity

 ! [NOTE] It looks like your app may be using a namespace other than "App".
 !
 !        To configure this and make your life easier, see: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#configuration

 Class name of the entity to create or update (e.g. OrangePopsicle):
 > Orange

[critical] Error thrown while running command "make:entity". Message: "Could not determine where to locate the new class "App\Entity\Orange", maybe try with a full namespace like "\My\Full\Namespace\Orange""

In Generator.php line 63:

  Could not determine where to locate the new class "App\Entity\Orange", maybe try with a full namespace like "\My\Full\Namespace\Orange"

Tried all internet solutions. Didn't work. I need to work with this version of Symfony. I can't upgrade it with latest.

Composer.json:

{
    "name": "invertus/academy",
    "type": "project",
    "license": "MIT",
    "description": "A minimal Symfony project recommended to create bare bones applications",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.1.7",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/doctrine-bundle": "^2.11",
        "doctrine/doctrine-migrations-bundle": "^3.3",
        "doctrine/orm": "^2.17",
        "runtime/frankenphp-symfony": "^0.2.0",
        "symfony/console": "6.4.*",
        "symfony/dotenv": "6.4.*",
        "symfony/flex": "^2",
        "symfony/framework-bundle": "6.4.*",
        "symfony/runtime": "6.4.*",
        "symfony/security-bundle": "6.4.*",
        "symfony/yaml": "6.4.*"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php73": "*",
        "symfony/polyfill-php74": "*",
        "symfony/polyfill-php80": "*",
        "symfony/polyfill-php81": "*",
        "symfony/polyfill-php82": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "6.4.*",
            "docker": true
        }
    },
    "require-dev": {
        "symfony/maker-bundle": "^1.52"
    }
}

I tried intalling orm-pack, doctrine-bundle, changing namespace in composer.json and kernel.php


Solution

  • A fix was changing namespace in all files to default "App". Link that helped my change all namespaces. I will try to show as clearly as possible:

    1. Update composer.json:
    {
      "autoload": {
        "psr-4": {
          /* "Some\\Name\\": "src/"  remove this */
          "App\\": "src/" /* add this */
        }
      },
      "autoload-dev": {
        "psr-4": {
         /* "Some\\Name\\Tests\\": "tests/" remove this */
         "App\\Tests\\": "tests/" /* add this */
        }
      }
    
    }
    
    1. Edit /src/Kernel.php file:
    // namespace Some\Name; remove this
    namespace App; // add this
    
    use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
    use Symfony\Component\HttpKernel\Kernel as BaseKernel;
    
    class Kernel extends BaseKernel
    {
        use MicroKernelTrait;
    }
    
    1. Edit bin/console file:
    #!/usr/bin/env php
    <?php
    
    // use Some\Name\Kernel; remove this
    use App\Kernel; // add this
    
    use Symfony\Bundle\FrameworkBundle\Console\Application;
    
    if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
        throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
    }
    
    require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
    
    return function (array $context) {
        $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
    
        return new Application($kernel);
    };
    
    1. Edit /public/index.php:
    // use Some\Name\Kernel; remove this
    use App\Kernel; // add this
    
    require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
    
    return function (array $context) {
        return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
    };
    
    1. Edit /config/services.yaml:
    /* Some\Name\: remove */
    App\: /* add */
            resource: '../src/'
            exclude:
                - '../src/DependencyInjection/'
                - '../src/Entity/'
                - '../src/Kernel.php'
    
    1. Edit /config/preload.php (may not be necessary because it will be correct):
    <?php
    // In if statement after __DIR__ preload file name before dot should be App_.....
    // Also in require should be the same
    if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
        require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
    }
    
    
    1. Edit /config/doctrine.yaml (in older symfony versions), in latest versions /config/packages/doctrine.yaml:
    doctrine:
      dbal:
        # dbal specific configuration
      orm:
        # orm specific configuration
        mappings:
          /* Cspray\MyCoolApp: remove */
             App: /* add */
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity'
              /* prefix: 'Some\Name\Entity' remove */
              prefix: App\Entity' /* add */
              alias: App
    
    1. Edit /bin/dev/maker.yaml. If you don't have folder /dev/ and file maker.yaml create it and write this:
    maker:
      root_namespace: 'App'
    

    Reference