php - Doctrine 2 multiple mappedBy? -


i've got problem setting doctrine mapping correctly.

i have cashregister entity has bin location , return bin location. both locations same type (binlocation entity).

outgoing cashregister, cashregister->getbinlocations() , cashregister->getreturnbinlocations() working fine, how can achieve binlocation->getcashregisters() returns cashregister entities referenced (binlocation + returnbinlocation)?

/**  * cashregister  *  * @orm\table(name="cash_registers")  * @orm\entity  * @orm\haslifecyclecallbacks  */ class cashregister {      ...      /**      * @var binlocation      *      * @orm\manytoone(targetentity="binlocation", inversedby="cashregisters")      * @orm\joincolumn(name="bin_location_id", referencedcolumnname="id")      */     private $binlocation;      /**      * @var binlocation      *      * @orm\manytoone(targetentity="binlocation", inversedby="cashregisters")      * @orm\joincolumn(name="return_bin_location_id", referencedcolumnname="id")      */     private $returnbinlocation;       /**      * @return binlocation      */     public function getbinlocation()     {         return $this->binlocation;     }      /**      * @return binlocation      */     public function getreturnbinlocation()     {         return $this->returnbinlocation;     }      ...  }  /**  * binlocation  *  * @orm\table(name="bin_locations")  * @orm\entity  * @orm\haslifecyclecallbacks  */ class binlocation {      ...      /**      * @var cashregister[]      *      * @orm\onetomany(targetentity="cashregister", mappedby="binlocation") <= here problem, in case mappedby need array [binlocation, returnbinlocation]      */     private $cashregisters;       /**      * @return cashregister[]      */     public function getcashregisters()     {         return $this->cashregisters;     }      ...  } 

the simple answer cannot. mappedby accepts 1 argument.

the solution achieve want simple. create second property in binlocation called: cashregisters2 follows:

/**  * @var cashregister[]  *  * @orm\onetomany(targetentity="cashregister", mappedby="binlocation")  */ private $cashregisters;  /**  * @var cashregister[]  *  * @orm\onetomany(targetentity="cashregister", mappedby="binlocation")   */ private $cashregisters2; 

then merge collections in getcashregisters method.

/**  * @return cashregister[]  */ public function getcashregisters() {     return new arraycollection(                       array_merge($cashregisters->toarray(), $cashregisters2->toarray())     ); } 

also change cashregister mappings accordingly:

/**  * @var binlocation  *  * @orm\manytoone(targetentity="binlocation", inversedby="cashregisters")  * @orm\joincolumn(name="bin_location_id", referencedcolumnname="id")  */ private $binlocation;  /**  * @var binlocation  *  * @orm\manytoone(targetentity="binlocation", inversedby="cashregisters2")  * @orm\joincolumn(name="return_bin_location_id", referencedcolumnname="id")  */ private $returnbinlocation; 

note: did not test code. example server guide only.

note2: arraycollection merge inspired here: https://stackoverflow.com/a/16871539/2853903


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -