doctrine2 - Two one-to-many relationship with reference table (Doctrine 2, ZF2) -
i've problem many-to-many relation. want have access reference table querybuilder query. many-to-many relation don't have access reference table, i've set 2 one-to-many relationships. structure likes:
user ---> userusercategory <--- usercategory
the above structure has 2 one-to-many relationships , working fine database. when have user following data in database (in userusercategory):
table user id | name 1 | bart 2 | gerard table category id | name 1 | officer 2 | medic table userusercategory user | category 1 | 1 2 | 2
so bart officer , gerard medic. when want retrieve data, said bart medic, , gerard has "null" value in category.
my user-entity:
/** * entity class representing post of our user module. * * @orm\entity * @orm\table(name="user") * @orm\entity(repositoryclass="user\repository\userrepository") * */ class user extends zfcuser implements userinterface { /** * categories user * * @orm\onetomany(targetentity="user\entity\userusercategory", mappedby="user_id", cascade={"remove", "persist"}) * @var userusercategory * @access protected */ protected $user_usercategories; //name & user_id comes here /** * constructor make new arraycollection addresses * * */ public function __construct() { $this->user_usercategories = new arraycollection(); } /** * @param collection $categories */ public function adduserusercategories(collection $user_usercategories) { foreach ($user_usercategories $user_usercategorie) { $user_usercategorie->setuser($this); $this->user_usercategories->add($user_usercategorie); } } /** * @param collection $categories */ public function removeuserusercategories(collection $user_usercategories) { foreach ($user_usercategories $user_usercategorie) { $user_usercategorie->setuser(null); $this->user_usercategories->removeelement($user_usercategorie); } } /** * @return collection */ public function getuserusercategories() { return $this->categories; } }
my usercategory-entity:
/** * user category entity. * @orm\entity * @orm\table(uniqueconstraints={@orm\uniqueconstraint(name="unique_name_parentid", columns={"name", "parent_id"})}) * @orm\haslifecyclecallbacks */ class usercategory extends category { /** * user_usercategories * * @orm\onetomany(targetentity="user\entity\userusercategory", mappedby="category_id") * @var userusercategory * @access protected */ protected $user_usercategories; /** * constructor */ public function __construct() { $this->user_usercategories = new arraycollection(); } /** * @param collection $categories */ public function adduserusercategories(collection $user_usercategories) { foreach ($user_usercategories $user_usercategorie) { $user_usercategorie->setcategory($this); $this->user_usercategories->add($user_usercategorie); } } /** * @param collection $categories */ public function removeuserusercategories(collection $user_usercategories) { foreach ($user_usercategories $user_usercategorie) { $user_usercategorie->setcategory(null); $this->user_usercategories->removeelement($user_usercategorie); } } /** * @return collection */ public function getuserusercategories() { return $this->categories; } }
my userusercategory-entity:
/** * entity class representing post of our user_usercategory entity. * * @orm\entity * @orm\table(name="user_usercategory") * */ class userusercategory { /** * user category * * @orm\manytoone(targetentity="user\entity\user", inversedby="user_usercategories") * @orm\joincolumn(name="user_id", referencedcolumnname="user_id", nullable=false, ondelete="cascade") * @orm\id * * @var user * @access protected */ protected $user_id; /** * category user * * @orm\manytoone(targetentity="user\entity\usercategory", inversedby="user_usercategories") * @orm\joincolumn(name="category_id", referencedcolumnname="id", nullable=false, ondelete="cascade") * @orm\id * * @var category * @access protected */ protected $category_id; public function getuser() { return $this->user; } /** * set user * * @param user $user * @return user */ public function setuser(user $user = null) { //die('setuser'); $this->user = $user; return $this; } public function getcategory() { return $this->category; } /** * set category * * @param category $category * @return category */ public function setcategory(category $category = null) { $this->category = $category; return $this; } }
when execute following line, gives wrong result. wrong category pops up:
\doctrine\common\util\debug::dump($this->getentitymanager()->find('user\entity\user', '49')->user_usercategories); die;
result:
array(1) { [0]=> object(stdclass)#452 (3) { ["__class__"]=> string(28) "user\entity\userusercategory" ["user_id"]=> string(16) "user\entity\user" ["category_id"]=> string(24) "user\entity\usercategory" } }
in category_id medic printed, expect officer back.
in other user, (id=60) category_id field "null". likes doctrine skips first input in userusercategory, starts second , can't last category anymore.
no offence, find code hard read. suggest few corrections , might in solving problem.
1: naming: instead of usercategory, rename category. if category have different types, create new column "type" values constansts like
class category { const type_user = 1 ; ....
2: instead of addcategories(collection $array), singular version like
public function addcategory(category $category) { $reference = new usercategory() ; $reference->setuser($this) ; $reference->setcategory($category) ; $this->user_categories->add($reference) ; } public function removecategory(category $category) { foreach($this->user_categories $reference) { if ( $reference->getcategory() === $category ) $this->user_categories->removeelement($reference) ; } }
symfony2 automaticaly recognizes methods this. if relation plural (like categories), s2 find singularified addcategory , removecategory methods.
to array of categories, use this:
public function getcategories() { $categories = new arraycollection() ; foreach($this->user_categories $reference) { $categories->add( $reference->getcategory() ) ; } return $categories ; }
if this, solve problem have.
Comments
Post a Comment