Home > Net >  Migrating symfony 3.4 to 4.4: Doctrine MappingException not a valid entity or mapped super class iss
Migrating symfony 3.4 to 4.4: Doctrine MappingException not a valid entity or mapped super class iss

Time:01-05

I've migrated from Symfony 3.4 to 4.4 and moved from using bundles to the /src/ directory. Doctrine throws an exception when I query an entity:

$regions = $this->getDoctrine()->getRepository(Regions::class);
$regionInfo = $regions->findOneBy(array('region' => strtolower($regionSearch)));

Result: Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "Class "App\Entity\Regions" is not a valid entity or mapped super class."

Here is the entity:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="Regions")
 */

/**
 * Regions
 */
class Regions
{
    /** properties ... */
}

I'm wondering whether this is because it can't find the orm.xml which was in bundle/Resources ? Also the use Doctrine\ORM\Mapping as ORM appears unused.

CodePudding user response:

Your annotations should be set right above the class declaration:

/**
 * Regions
 * @ORM\Entity
 * @ORM\Table(name="Regions")
 */
class Regions

With your current file, only the first docblock is read and as it is missing the @ORM\Entity annotation, Doctrine considers it isn't a valid entity class.

  •  Tags:  
  • Related