symfony - Symfony2 - Share Entity Between Bundles with different relationships -


how share entity between multiple bundles different relationships?

for example both zooanimalbundle , farmanimalbundle need user entity. third bundle accountuserbundle has user entity.

in both zoo , farm animalbundles create user entity so:

use account\userbundle\entity\user baseuser;   class user extends baseuser  { } 

i have hospital entity in zoo:

class hospital { /**  * @orm\manytomany(targetentity="zoo\anaimalbundle\entity\user")  * @orm\jointable(name="users_zoo_animals")  */ protected $users;  

and room entity in farm:

class room { /**  * @orm\manytomany(targetentity="farm\anaimalbundle\entity\user")  * @orm\jointable(name="users_farm_animals")  */ protected $users;  

everything works far in can call zoo.room->getusers() or farm.hospital->getusers()

however problem i'm not sure on how set inverse relationship in respective user entities.

if example update farmanimal user entity , run doctrine:generate:entities

/**  * @orm\entity  */ class user extends baseuser {     /**      * @orm\manytomany(targetentity="room", mappedby="users", cascade={"persist"})      */     protected $rooms; } 

it copy protected $properties baseuser , create set , methods not want. correct way of setting these relationships?

update

if don't setup inverse relationship, how select users hospital.id = 1

    $qb = $this->getentitymanager()->createquerybuilder()         ->select(             'u'         )         ->from('account\userbundle\entity\user','u')         ->leftjoin('u.hospitals', 'h')         ->andwhere('h.id = :hospital_id')             ->setparameter('hospital_id',$hospital_id); 

this gives error:

   class account\userbundle\entity\user has no association named hospitals  

i know select hospital , join user because relationship exist need select users because using them doctrine\orm\tools\pagination\paginator

the query be

    $qb = $this->createquerybuilder('a')         ->select(             'h', 'u'         )         ->leftjoin('h.users', 'u') 

the problem paginator sees 1 result hospital because users attached it.

you can define abstract entity dependencies , implement them other bundles.

first, each of bundles depending on user entity should define user interface. example:

namespace foo\barbundle\entity;  interface userinterface {     public function getid();      public function getemail();      // other getters } 

then, in each entity depending on user, define relationship, e.g.:

namespace foo\barbundle\entity;  use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert;  /**  * @orm\entity  */ class {     /**      * @orm\manytoone(targetentity="userinterface")      * @assert\notnull      */     protected $user;      // add other fields required } 

now need register user entity implementation of userinterfaces:

namespace foo\userbundle\entity;  use foo\barbundle\entity\userinterface baruserinterface; use foo\foobarbundle\entity\userinterface foobaruserinterface;  /**  * @orm\entity  */ class user implements baruserinterface, foobaruserinterface {     // implement demanded methods } 

then add following app/config/config.yml:

doctrine:     orm:         resolve_target_entities:             foo\barbundle\entity\userinterface: foo\userbundle\entity\user             foo\fooarbundle\entity\userinterface: foo\userbundle\entity\user 

(heads up: there doctrine.orm node you'll have extend.)


this not perfect solution, because cannot fields user entity should have. on other hand, it's strictly oop, don't have know internals of user implementation – need return right values.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -