John Davidson

php - Doctrine mapping not recognized with more complex hierarchy

0 comments
Message:


Given the following entity hierachy:


// in main project src/Entity
#[ORM\Entity]
class Article extends AbstractArticle

// in bundle src/Entity/Content
#[ORM\MappedSuperclass]
abstract class AbstractArticle extends AbstractEntity

// in bundle src/Entity
#[ORM\MappedSuperclass]
abstract class AbstractEntity implements NormalizableInterface, EntityInterface

And this configuration:


orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
XyBundle:
is_bundle: true
type: annotation
dir: 'Entity'
prefix: 'XyBundle\Entity'
alias: Xy

I'm getting this error:


Class "App\Entity\Article" sub class of "XyBundle\Entity\Content\AbstractArticle" is not a valid entity or mapped super class.


I want to instantiate one or more different articles of the abstract article in the future. Yet, this configuration works if and only if I put #[ORM\Entity] to the AbstractArticle class.


What am I doing wrong here?



My Answer:
php
// src/Entity/BaseEntity.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\MappedSuperclass
*/
class BaseEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;

// other common properties and methods
}


php
// src/Entity/User.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class User extends BaseEntity
{
/**
* @ORM\Column(type="string")
*/
private $username;

// other user-specific properties and methods
}


php
// src/Entity/Admin.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Admin extends User
{
/**
* @ORM\Column(type="string")
*/
private $role;

// other admin-specific properties and methods
}


php
// src/Entity/Manager.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Manager extends User
{
/**
* @ORM\Column(type="string")
*/
private $department;

// other manager-specific properties and methods
}


php
// src/Entity/Employee.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Employee extends User
{
/**
* @ORM\Column(type="string")
*/
private $position;

// other employee-specific properties and methods
}


In this hierarchy, `User` is a subclass of `BaseEntity`, and `Admin`, `Manager`, and `Employee` are subclasses of `User`. Each subclass adds specific properties and methods.

When using Doctrine, make sure to properly configure the mapping annotations in each entity class to define the database schema. Also, ensure that the entity classes are correctly located in the `src/Entity` directory of your project.

Rate this post

3 of 5 based on 2946 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us