Doctrine "Entity" is not a valid entity or mapped super class
Asked Answered
S

1

5

In my doctrine.php I have the following configuration

<?php

// See https://symfony.com/doc/current/reference/configuration/framework.html

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $container) {
    $container->extension('doctrine', [
        'dbal' => [
            'url' => '%env(DATABASE_URL)%',
        ],
        'orm' => [
            'auto_generate_proxy_classes' => true,
            'auto_mapping' => true,
            'mappings' => [
                'default' => [
                    'is_bundle' => false,
                    'type' => 'annotation',
                    'dir' => '%kernel.project_dir%/src/Entity',
                    'prefix' => 'App\Entity',
                    'alias' => 'App'
                ]
            ]
        ]
    ]);
};

And my Entity class is defined as follows

namespace App\Entity;

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;

#[Entity(repositoryClass: EntityRepository::class)]
#[Table(name: 'entity')]
class Entity

...

I tried accessing it like by calling getRepository like this

$entityRepository = $entityManager->getRepository(Entity::class);

But this fails with the is not a valid entity or mapped super class

P.S.

If necessary, this is my EntityRepository.php

<?php

namespace App\Repository;

use App\Entity\Entity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class EntityRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Entity::class);
    }
}
Spurious answered 23/8, 2022 at 12:24 Comment(2)
Maybe you need Quoting Reserved Words? ie name: 'entity'...or choose another name to test.Grenadine
Oh it is different name than entity. I am just using that as reference. It is in fact Country table with CountryRepository. But since that was not relevant to the question I abstracted itSpurious
S
13

I found an issue. I am a dummy..

I had to change

'type' => 'annotation'

to

'type' => 'attribute'

Since I'm using php8 syntax. Sorry lads!

Spurious answered 23/8, 2022 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.