Symfony 2 / Doctrine 2: two objects for one table, use one in favor of the other

In my Symfony2 application, I extracted most of my Entities into a separate library that I install using the composer.

This library is not dependent on Symfony2 (but depends on Doctrine because I use annotations) because I would like to use it in other projects than Symfony2.

The library contains a ClientUser Entity object that maps to the client_users table. In my Symfony2 application, I would like to use the same ClientUser Entity for authentication. This requires me to implement Symfony\Component\Security\Core\User\UserInterface .

The problem is that I would like to have both a "Symfony2-agnostic" and a "Symfony-aware" ClientUser Entity implementation (which should both appear in the same table). I tried extending both classes from the ClientUserAbstract Entity object, but that did not work.

 <?php namespace My\Library\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\MappedSuperClass */ class ClientUserAbstract { // all my fields here } 

My "Symfony2-agnostic" Entity:

 <?php namespace My\Library\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class ClientUser extends ClientUserAbstract { // nothing here, it empty } 

My "Symfony2-aware" Entity:

 <?php namespace Vendor\Bundle\MyBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; /** * @ORM\Entity */ class ClientUser extends ClientUserAbstract implements UserInterface { // all methods that Symfony requires because of the UserInterface here: public function getRoles(); public function getPassword(); public function getSalt(); public function getUsername(); public function eraseCredentials(); } 

My Symfony 2 now detects two objects that point to the same table and do not work with Exception. I either need to tell my Symfony2 application to β€œignore” my My\Library\Entity\ClientUser , or I need a way to expand it. Any ideas?

+5
source share
1 answer

Just in case, if anyone else has this problem, here is my comment converted to answer:

For a given control object, it is strictly one object in the table. You need to create a second object manager and use it for authentication.

Plus, of course, I like to get replays.

+12
source

Source: https://habr.com/ru/post/951620/


All Articles