How to extend with Doctrine-Entity without a column delimiter

Task:

I want to reuse the existing entity of a third-party package with class inheritance in such a way that only one table remains, and extra things are not needed. This means: no column discriminator and no JOINs. Instead, only the final most inherited class should be available for query, add some properties to the base object and just use a single table containing all the columns that are added to the entity from itself and through inheritance.

To be clear here: I'm not interested in classic table inheritance. I just want to extend the base class with additional columns so that the table in the database represents the sum of all the necessary columns.

There is no need to instantiate the base object.

For those who are interested, I will explain the reason below.

The base object of the third-party library:

Sylius\UserEntity (TableName: "sylius_user") ============================================ ColA, ColB, ColC 

My class:

 MyProject\UserEntity : Sylius\UserEntity (TableName: "user") <---- overwrite the base table name ======================================== ColD, ColE, ColF 

The model presented above represents the final approach: my user object extends the syslius user object and must be saved and retrieved from the user table (instead of user and sylius_user), which contains all the columns of the final extended object:

Table Structure:

Thus, in my szenario there will only be one table.

 Table "user": ============= ColA, ColB, ColC, ColD, ColE, ColF 

The first 3 columns are properties in the base object, and the last 3 columns are properties in the "my" user object, which receives the first three inheritances.

What I've done:

My user class looks like this:

 use \Sylius\Component\User\Model\User as BaseUser; /** * User * * @ORM\Entity(repositoryClass="MyAppName\UserBundle\Repository\UserRepository") * @ORM\Table(name="user", uniqueConstraints= ... */ class User extends BaseUser { 

UserRepository:

 class UserRepository extends EntityRepository { /** * @param string $usernameOrEmail * @return User */ public function findByUsernameOrEmail($usernameOrEmail) { $qb = $this ->createQueryBuilder('u') ->where('u.username = :search OR u.email = :search') ->setParameter('search', $usernameOrEmail); try { return $qb->getQuery()->getSingleResult(); } catch(NoResultException $e) { return null; } } } 

This query selects columns from the user table, but the query attempts to select columns twice each with a separate table alias. The received request will fail because it is broken.

What Doctrine does here is:

 SELECT s0.ColA, s0.ColB, s0.ColC, u0.ColD, u0.ColE, u0.ColF FROM user u0 

As you can see everything, an additional alias of the table "s0" is used here without a link to the table. What I wanted the doctrine to do:

 SELECT u0.ColA, u0.ColB, u0.ColC, u0.ColD, u0.ColE, u0.ColF FROM user u0 

Ho for this?

For those who are interested in the goal of this task:

We want to add sylius packages to our long-standing Symfony application, which already has its own set of users with a user model class and existing data.

So, we would like to extend the sylius custom class by adding our own properties to build a โ€œbridgeโ€ between the sylius class and our custom class. The difference between them is subtle on the property side and consists of only a few columns to rename and add some special properties and methods. But we have a lot of relationships from our user class to other objects, which will not be a problem if we can doctrines ignore table inheritance staff and just act like a regular thing to reuse here.

+7
php mysql symfony doctrine2 sylius
source share
1 answer

Is the base class a MappedSuperclass ?

While the base class is defined as MappedSuperclass , you can simply extend the class and define the extending class as Entity .

Sylius defines entities by default as MappedSuperclass. The subscriber ( LoadOrmMetadataSubscriber ) passes each object and, if necessary, sets the MappedSuperclass to false, which means that it changes the MappedSuperclass to Entity when defining the class in the config.

+1
source share

All Articles