php - How to manage a ManyToOne relation with symfony 2 and doctrine -


i’m discovering symfony 2 , training myself on wonderful framework. have newbie questions...

i followed tutorial in order manage relations doctrine : http://symfony.com/doc/current/book/doctrine.html

well, during tests had question , problem.

first question: symphony best practices says it’s better use annotations doctrine mapping instead of yaml in other .orm.yml file. good, totally agree that. "using annotations convenient , allows define in same file". if choose use annotations, guess not need .orm.yml mapping file anymore? suppose have table named usertype. have created entity class usertype.php , added mapping information annotations directly in class. try, adding statement:

use doctrine\orm\mapping orm; 

or not in usertype.php class, symfony display error:

no mapping file found named 'usertype.orm.yml' class 'appbundle\entity\usertype 

if don‘t create usertype.orm.yml file.

so question: if choose use annotations, still need .orm.yml mapping file? if yes, minimum information must put in file?

second point problem: have users roles in different table (usertype) user table.

+-------------------------------------------+-------------------------------------------+ |user                                       |   usertype                                | |id    username    password    usertype_id  |   usertype_id  label          role        | |1     testuser      1            |   1            administrator  role_admin  | +-------------------------------------------+-------------------------------------------+ 

so suppose need add manytoone relation in user.php entity class (many users can have same 1 role).

so added thoses lines in user.orm.yml mapping file

(…) manytoone:         usertype:             targetentity: usertype             joincolumn:                 name: usertype_id                 referencedcolumnname: usertype_id     lifecyclecallbacks: {  } (…) 

and declared var usertype in user.php entity class this

/** * */ private $usertype; 

this usertype.php entity class file:

    namespace appbundle\entity;      use doctrine\orm\mapping orm;      /**      * @orm\entity      */     class usertype     {         /**          * @orm\id          * @orm\column(type="integer", name="usertype_id")          * @orm\generatedvalue(strategy="auto")          */         private $id;          /**          * @orm\column(type="string", name="label")          */         private $label;           /**          * @orm\column(type="simple_array", name="role")          */         private $role;           public function getid()         {             return $this->id;         }          /**          *          */         public function getlabel()         {             return $this->label;         }         public function setlabel($label)         {             $this->label = $label;         }          /**          *          */         public function getrole()         {             return $this->role;         }         public function setrole($role)         {             $this->role = $role;         } } 

and usertype.orm.yml file:

appbundle\entity\usertype:     type: entity     table: usertype     id:         id:             type: integer             id: true             column: usertype_id             generator:                 strategy: auto     fields:         label:             type: string             length: '150'             column: label         role:             type: simple_array             length: '100'             column: role     lifecyclecallbacks: {  } 

ok seems $usertype var of user.php entity object containing usertype data. if a

var_export($this->usertype, true); 

of $usertype object got :

proxies\__cg__\appbundle\entity\usertype:: __set_state(array( '__initializer__' => closure:: __set_state(array( )), '__cloner__' => closure::__set_state(array( )), '__isinitialized__' => false, 'id' => 1, 'label' => null, 'role' => null, )) 

only field id filled others null. how can label , role field values in object $usertype? mistake?

many help.

thanks advices. helped me lot.

i solved 2 troubles.

for .orm files problem : indeed @chalasr advised, after deleting resources/config/doctrine folder, symphony stopped showing me error regarding missing .orm files. following @chalasr other tips managed stop using orm files. thanks.

but still had other problem (only "id "field filled in $usertype object others null). notice $usertype object not initialized , in fact doctrine “lazy-loading-issue ».

these 2 posts helped me solve problem:

get entities unidirectional many many relation doctrine2 , symfony2

doctrine2 association not initialized

i added option fetch="eager" manytoone relation , $usertype object fields filled.

this actual manytoone relation (made annotations ;-))

/**      * @orm\manytoone(targetentity="usertype", fetch="eager")      * @orm\joincolumn(name="users_types_id", referencedcolumnname="users_types_id")      */     private $usertype; 

and how role field value

/**      * returns roles or permissions granted user security.      */     public function getroles()     {          $accessor_user_role = propertyaccess::createpropertyaccessor();          $roles = $accessor_user_role->getvalue($this->usertype, 'role');          // guarantees user has @ least 1 role security         if (empty($roles)) {             $roles[] = 'role_user';         }          return $roles;     } 

i hope respect symfony practices proceeding in way.

many help.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -