-
-
Save Ocramius/3121916 to your computer and use it in GitHub Desktop.
<?php | |
use Doctrine\ORM\Mapping as ORM; | |
use Doctrine\Common\Collections\ArrayCollection; | |
/** | |
* @ORM\Entity() | |
* @ORM\Table(name="user") | |
*/ | |
class User | |
{ | |
/** | |
* @var int|null | |
* @ORM\Id() | |
* @ORM\GeneratedValue(strategy="AUTO") | |
* @ORM\Column(type="integer", name="id") | |
*/ | |
protected $id; | |
/** | |
* @var \Doctrine\Common\Collections\Collection|UserGroup[] | |
* | |
* @ORM\ManyToMany(targetEntity="UserGroup", inversedBy="users") | |
* @ORM\JoinTable( | |
* name="user_usergroup", | |
* joinColumns={ | |
* @ORM\JoinColumn(name="user_id", referencedColumnName="id") | |
* }, | |
* inverseJoinColumns={ | |
* @ORM\JoinColumn(name="usergroup_id", referencedColumnName="id") | |
* } | |
* ) | |
*/ | |
protected $userGroups; | |
/** | |
* Default constructor, initializes collections | |
*/ | |
public function __construct() | |
{ | |
$this->userGroups = new ArrayCollection(); | |
} | |
/** | |
* @param UserGroup $userGroup | |
*/ | |
public function addUserGroup(UserGroup $userGroup) | |
{ | |
if ($this->userGroups->contains($userGroup)) { | |
return; | |
} | |
$this->userGroups->add($userGroup); | |
$userGroup->addUser($this); | |
} | |
/** | |
* @param UserGroup $userGroup | |
*/ | |
public function removeUserGroup(UserGroup $userGroup) | |
{ | |
if (!$this->userGroups->contains($userGroup)) { | |
return; | |
} | |
$this->userGroups->removeElement($userGroup); | |
$userGroup->removeUser($this); | |
} | |
} |
<?php | |
use Doctrine\ORM\Mapping as ORM; | |
use Doctrine\Common\Collections\ArrayCollection; | |
/** | |
* @ORM\Entity() | |
* @ORM\Table(name="usergroup") | |
*/ | |
class UserGroup | |
{ | |
/** | |
* @var int|null | |
* @ORM\Id() | |
* @ORM\GeneratedValue(strategy="AUTO") | |
* @ORM\Column(type="integer", name="id") | |
*/ | |
protected $id; | |
/** | |
* @var \Doctrine\Common\Collections\Collection|User[] | |
* | |
* @ORM\ManyToMany(targetEntity="User", mappedBy="userGroups") | |
*/ | |
protected $users; | |
/** | |
* Default constructor, initializes collections | |
*/ | |
public function __construct() | |
{ | |
$this->users = new ArrayCollection(); | |
} | |
/** | |
* @param User $user | |
*/ | |
public function addUser(User $user) | |
{ | |
if ($this->users->contains($user)) { | |
return; | |
} | |
$this->users->add($user); | |
$user->addUserGroup($this); | |
} | |
/** | |
* @param User $user | |
*/ | |
public function removeUser(User $user) | |
{ | |
if (!$this->users->contains($user)) { | |
return; | |
} | |
$this->users->removeElement($user); | |
$user->removeUserGroup($this); | |
} | |
} |
@kemo I think I found a way to improve performance for large tables. It looks like by default $collection->contains
loads all the related objects in memory. I set the fetch
option (in the ManyToMany binding) to EXTRA_LAZY
and that seemed to reduce the loaded entities quite drastically! See: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/extra-lazy-associations.html
@haydenk a specific return value is outside the scope for this gist, which is simply demonstrating how to handle the bi-directional object associations.
Ultimately the return values would depend on your desired behavior. You could also return $this;
to allow for method chaining,
or return $this->someOtherMthod($object);
to perform additional business logic and it's return value. Really is up to you.
@haydenk I also usually do returns as soon as I can so the processor doesn't have to read the entire function before continuing.
Also, not doing that you often see unneeded extra indentation in your whole function.
A couple questions, maybe this is ignorance but...
true
orfalse
like the Collection methods do?I see this a lot
And I am just curious, why not this instead?
This way, no matter what, the add and remove methods always return a boolean.
I am genuinely curious if there is some compatibility reasons behind it or if some of that functionality just didn't exist at one time and everyone is used to it or whatever it may be?