Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/0a176fb34a6bb72df41d to your computer and use it in GitHub Desktop.
Save doctrinebot/0a176fb34a6bb72df41d to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-849 - https://github.com/doctrine/doctrine2/issues/5369
<?php
/**
* @Entity
* @Table(name = "user")
* @property string $name
* @property-write string $password
* @property-read Doctrine\Common\Collections\Collection $roles
*/
class UserEntity
{
//...
/**
* @ManyToMany(targetEntity = "RoleEntity")
* @JoinTable(name = "user_role",
* joinColumns = {@JoinColumn(name = "user")},
* inverseJoinColumns = {@JoinColumn(name = "role")}
* )
* @var Doctrine\Common\Collections\Collection
*/
private $roles;
public function __construct() {
//...
$this->roles = new ArrayCollection;
}
public function getRoles() {
return $this->roles;
}
public function addRole(RoleEntity $entity) {
if (!$this->roles->contains($entity)) {
$this->roles[] = $entity;
}
return $this;
}
public function clearRoles() {
$this->roles->clear(); //This should remove all roles, $this->roles->count() should always be 0
if ($this->roles->count() > 0) { //but it isn't
throw new Exception("Roles are not empty after clear.");
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment