donde el empleado puede tener varios roles y un rol puede estar asignado a varios empleados.
en una de las entidades se define la relacion, por ejemplo en Employee
para ayudarnos en la creación de la entidad vamos a usar la consola:
php app/console doctrine:generate:entity
donde podremos ir agregando el nombre, bajo el formato AppBundle:Employee
y los atributos de la entidad.
esto generará 2 archivos por entidad: entity/Employee.php y repository/EmployeeRepository.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Role;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Employee
*
* @ORM\Table(name="employee")
* @ORM\Entity(repositoryClass="AppBundle\Repository\EmployeeRepository")
*/
class Employee
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="address", type="string", length=255, nullable=true)
*/
private $address;
/**
* @var int
*
* @ORM\Column(name="dni", type="integer", unique=true)
*/
private $dni;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Role")
* @ORM\JoinTable(name="employees_roles",
* joinColumns={@ORM\JoinColumn(name="employee_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $roles;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Employee
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* @param string $address
* @return Employee
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set dni
*
* @param integer $dni
* @return Employee
*/
public function setDni($dni)
{
$this->dni = $dni;
return $this;
}
/**
* Get dni
*
* @return integer
*/
public function getDni()
{
return $this->dni;
}
/**
* Get roles as array of strings.
*
* @return array
*/
public function getRoles() {
$roleNames = [];
foreach ($this->roles as $role) {
$roleNames[] = $role->getName();
}
return $roleNames;
}
/**
* Get roles ass ArrayCollection.
*
* @return ArrayCollection
*/
public function getRolesCollection() {
return $this->roles;
}
/**
* Returns TRUE if employee has ROLE_ADMIN.
*
* @return bool
*/
public function isAdmin() {
return in_array('ROLE_ADMIN', $this->getRoles(), TRUE);
}
/**
* Set roles.
*
* @param ArrayCollection $roles
* @return Employee
*/
public function setRolesCollection($roles) {
$this->roles = $roles;
return $this;
}
/**
* Add roles
*
* @param Role $roles
* @return Employee
*/
public function addRolesCollection(Role $roles) {
$this->roles[] = $roles;
return $this;
}
/**
* Remove roles
*
* @param Role $roles
*/
public function removeRolesCollection(Role $roles) {
$this->roles->removeElement($roles);
}
/**
* Constructor.
*/
public function __construct() {
$this->roles = new ArrayCollection();
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Role
*
* @ORM\Table(name="role")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RoleRepository")
*/
class Role
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
No hay comentarios:
Publicar un comentario