I'm writing a feature which calls for the records of my joining table to carry extra metadata (Joining-Table with Metadata). I've attempted to implement this in accordance with this section of the Doctrine documentation.
See below for example Entity definitions.
The challenge now is that getGroups
and setGroups
do not yield/set Group
entities (& the same is true from the Group
instance perspective), but they yield GroupUser
entities.
This adds a substantial delay to process of managing this relationships, which so far have been extremely smooth - for example, I cannot simply add, remove, or check for the existence of a Group
to the collection which getGroups
yields.
Can anyone identity any errors in my implementation, or else recommend a more fluid way of implementing this concept?
Thanks in advance for any input.
EDIT:
My main concern is this: using this implementation, retrieving a collection of Users from a Group entity requires this Entity method's mediation:
public function getUsers() {
return $this->users->map(function($groupUser){
return $groupUser->getUser();
});
}
I'm concerned that this could imply a major performance hit down the road. Am I incorrect?
Furthermore, how does one re-implement the setUsers
method?
Group entity:
<?php
/**
* @Entity
* @Table(name="group")
*/
class Group {
/**
* @Column(type="integer", nullable=false)
* @Id
*/
protected $id = null;
/**
* @OneToMany(targetEntity="GroupUser", mappedBy="group")
* @var \Doctrine\Common\Collections\Collection
*/
protected $users;
}
User entity:
<?php
/**
* @Entity
* @Table(name="user")
*/
class User {
/**
* @Column(type="integer", nullable=false)
* @Id
*/
protected $id = null;
/**
* @OneToMany(targetEntity="GroupUser", mappedBy="user")
* @var \Doctrine\Common\Collections\Collection
*/
protected $groups;
}
Joining entity:
<?php
/**
* @Entity
* @Table(name="group_user")
*/
class GroupUser {
/**
* @Id
* @ManyToOne(targetEntity="User", inversedBy="groups")
* @JoinColumn(name="userId", referencedColumnName="id")
*/
protected $user;
/**
* @Id
* @ManyToOne(targetEntity="Group", inversedBy="users")
* @JoinColumn(name="groupId", referencedColumnName="id")
*/
protected $group;
/**
* @Column(type="integer")
*/
protected $relationship;
}
Related -
- Same goal, slightly different approach, which consistently produced errors once the resulting collections were manipulated: http://www.doctrine-project.org/jira/browse/DDC-1323
- Supports the approach, no technical details: Doctrine 2 join table + extra fields