Why maps bundle class when should not? #6192

Closed
opened 2026-01-22 15:28:36 +01:00 by admin · 5 comments
Owner

Originally created by @BonBonSlick on GitHub (Mar 1, 2019).

Originally assigned to: @Ocramius on GitHub.

Q A
doctrine/annotations                   v1.6.0                           Docblock Annotations Parser
doctrine/cache                         v1.8.0                           Caching library offering an object-oriented API for many cache backends
doctrine/collections                   v1.5.0                           Collections Abstraction library
doctrine/common                        v2.10.0                          PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, ...
doctrine/data-fixtures                 v1.3.1                           Data Fixtures for all Doctrine Object Managers
doctrine/dbal                          v2.9.2                           Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/doctrine-bundle               1.10.2                           Symfony DoctrineBundle
doctrine/doctrine-cache-bundle         1.3.5                            Symfony Bundle for Doctrine Cache
doctrine/doctrine-fixtures-bundle      3.1.0                            Symfony DoctrineFixturesBundle
doctrine/doctrine-migrations-bundle    v2.0.0                           Symfony DoctrineMigrationsBundle
doctrine/event-manager                 v1.0.0                           Doctrine Event Manager component
doctrine/inflector                     v1.3.0                           Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                  1.1.0                            A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                         v1.0.1                           Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                    v2.0.0                           PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and eas...
doctrine/orm                           v2.6.3                           Object-Relational-Mapper for PHP
doctrine/persistence                   v1.1.0                           The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.

Support Question

We have own package bundle with abstract entity and default preset fields.

...
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;

/**
 * Class AbstractFaq.
 */
abstract class AbstractFaq implements FaqInterface, TimeStampableInterface, AuditLoggableInterface
{
    use TimestampableTrait;
    use AuditLoggableTrait;

    /**
     * @var string
     *
     * @ORM\Column(type="text")
     */
    protected $category;

    /**
     * AbstractFaq constructor.
     *
     * @param string $category
     *
     * @throws \Exception
     */
    public function __construct(
        string $category
    ) {
        $this->id = Uuid::uuid4();
        $this
            ->setCategory($category)
            ->setSequence($sequence);
    }

    /**
     * @return string
     */
    public function __toString(): string
    {
        return $this->getQuestion();
    }

    /**
     * {@inheritdoc}
     */
    public function getCategory(): string
    {
        return $this->category;
    }

To make it work in project we create entity eg

namespace App\Entity;

use PackageName\BundleName\Entity\AbstractFaq;
use Doctrine\ORM\Mapping as ORM;

/**
 * Class Faq.
 *
 * @ORM\Table(name="faq")
 * @ORM\Entity
 */
class Faq extends AbstractFaq
{
}
 

Exception during migration diff

[2019-03-01 15:14:29] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "Class "PackageName\BundleName\Entity\AbstractFaq" is not a valid entity or mapped super class." at /srv/oppasland/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php line 346 {"exception":"[object] (Doctrine\\ORM\\Mapping\\MappingException(code: 0): Class ....

Tell me please what I am missing?

Originally created by @BonBonSlick on GitHub (Mar 1, 2019). Originally assigned to: @Ocramius on GitHub. <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ----- ``` doctrine/annotations v1.6.0 Docblock Annotations Parser doctrine/cache v1.8.0 Caching library offering an object-oriented API for many cache backends doctrine/collections v1.5.0 Collections Abstraction library doctrine/common v2.10.0 PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, ... doctrine/data-fixtures v1.3.1 Data Fixtures for all Doctrine Object Managers doctrine/dbal v2.9.2 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management. doctrine/doctrine-bundle 1.10.2 Symfony DoctrineBundle doctrine/doctrine-cache-bundle 1.3.5 Symfony Bundle for Doctrine Cache doctrine/doctrine-fixtures-bundle 3.1.0 Symfony DoctrineFixturesBundle doctrine/doctrine-migrations-bundle v2.0.0 Symfony DoctrineMigrationsBundle doctrine/event-manager v1.0.0 Doctrine Event Manager component doctrine/inflector v1.3.0 Common String Manipulations with regard to casing and singular/plural rules. doctrine/instantiator 1.1.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. doctrine/migrations v2.0.0 PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and eas... doctrine/orm v2.6.3 Object-Relational-Mapper for PHP doctrine/persistence v1.1.0 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share. ``` <!-- Before asking question here, please try asking on Gitter or Slack first. Find out more about Doctrine support channels here: https://www.doctrine-project.org/community/ Keep in mind that GitHub is primarily an issue tracker. --> ### Support Question <!-- Describe the issue you are facing here. --> We have own package bundle with abstract entity and default preset fields. ``` ... use Doctrine\ORM\Mapping as ORM; use Ramsey\Uuid\Uuid; /** * Class AbstractFaq. */ abstract class AbstractFaq implements FaqInterface, TimeStampableInterface, AuditLoggableInterface { use TimestampableTrait; use AuditLoggableTrait; /** * @var string * * @ORM\Column(type="text") */ protected $category; /** * AbstractFaq constructor. * * @param string $category * * @throws \Exception */ public function __construct( string $category ) { $this->id = Uuid::uuid4(); $this ->setCategory($category) ->setSequence($sequence); } /** * @return string */ public function __toString(): string { return $this->getQuestion(); } /** * {@inheritdoc} */ public function getCategory(): string { return $this->category; } ``` To make it work in project we create entity eg namespace App\Entity; use PackageName\BundleName\Entity\AbstractFaq; use Doctrine\ORM\Mapping as ORM; ``` /** * Class Faq. * * @ORM\Table(name="faq") * @ORM\Entity */ class Faq extends AbstractFaq { } ``` Exception during migration diff `[2019-03-01 15:14:29] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "Class "PackageName\BundleName\Entity\AbstractFaq" is not a valid entity or mapped super class." at /srv/oppasland/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php line 346 {"exception":"[object] (Doctrine\\ORM\\Mapping\\MappingException(code: 0): Class ....` Tell me please what I am missing?
admin added the Question label 2026-01-22 15:28:36 +01:00
admin closed this issue 2026-01-22 15:28:36 +01:00
Author
Owner

@BonBonSlick commented on GitHub (Mar 1, 2019):

Found solution, the reason is that in our own, custom package, bundle we do


    /**
     * FaqRepository constructor.
     *
     * @param EntityManagerInterface $entityManager
     */
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->repository = $entityManager->getRepository(Faq::class);
    }

But have to map to entity in project, eg App\Entity\Faq.
But this way we bind entity path. Is there other way?

@BonBonSlick commented on GitHub (Mar 1, 2019): Found solution, the reason is that in our own, custom package, bundle we do ``` /** * FaqRepository constructor. * * @param EntityManagerInterface $entityManager */ public function __construct(EntityManagerInterface $entityManager) { $this->repository = $entityManager->getRepository(Faq::class); } ``` But have to map to entity in project, eg App\Entity\Faq. But this way we bind entity path. Is there other way?
Author
Owner

@Ocramius commented on GitHub (Mar 1, 2019):

But this way we bind entity path

Could you elaborate a bit more? This isn't clear to me.

@Ocramius commented on GitHub (Mar 1, 2019): > But this way we bind entity path Could you elaborate a bit more? This isn't clear to me.
Author
Owner

@BonBonSlick commented on GitHub (Mar 4, 2019):

We have created bundle alike FOSUser.
There is entity in that package called AbstractFaq.
When some project installs package it has to create own FAQ entity and extend AbstractFaq form the bundle.
AbstractFaq has mappings as annotations. (This way simpler to maintain because package is small).
FosUser and FosUser mapping
Validation is made with DTO

Issue is, in FOS user bundle, repository get by class
please note there is no checks if this is subclass or interface. FOS binds class with configs.
No checks or typehints like FaqInterface or UserInterface when getting repository with getRepository.
To my mind this way is error prone.

As I described above, we bind, hardcode for typehinting.

// this class is in project, not in package! This namespace from application.
 use App\Entity\ProjectEntity; 
...
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->repository = $entityManager->getRepository(ProjectEntity::class);
    }
// build queries

Is there any other way to know path to entity in project and load it to create query builder, query?

@BonBonSlick commented on GitHub (Mar 4, 2019): We have created bundle alike FOSUser. There is entity in that package called AbstractFaq. When some project installs package it has to create own FAQ entity and extend AbstractFaq form the bundle. AbstractFaq has mappings as annotations. (This way simpler to maintain because package is small). [FosUser](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/config/doctrine-mapping/User.orm.xml) and [FosUser mapping](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Model/User.php) Validation is made with DTO Issue is, in FOS user bundle, repository [get by class](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Doctrine/UserManager.php) please note there is no checks if this is subclass or interface. [FOS binds class with configs](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/8e723f2c7a36191b0fd7db60d8f8d0aacc20d807/Resources/config/doctrine.xml). No checks or typehints like FaqInterface or UserInterface when getting repository with getRepository. To my mind this way is error prone. As I described above, we bind, hardcode for typehinting. ``` // this class is in project, not in package! This namespace from application. use App\Entity\ProjectEntity; ... public function __construct(EntityManagerInterface $entityManager) { $this->repository = $entityManager->getRepository(ProjectEntity::class); } // build queries ``` Is there any other way to know path to entity in project and load it to create query builder, query?
Author
Owner
@Ocramius commented on GitHub (Mar 4, 2019): See https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/resolve-target-entity-listener.html
Author
Owner

@BonBonSlick commented on GitHub (Mar 8, 2019):

@Ocramius Thank you!

@BonBonSlick commented on GitHub (Mar 8, 2019): @Ocramius Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6192