src/Entity/Role.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\RoleRepository")
  8.  *
  9.  * @ORM\HasLifecycleCallbacks()
  10.  */
  11. class Role
  12. {
  13.     public function __construct()
  14.     {
  15.         $this->users = new ArrayCollection();
  16.     }
  17.     /**
  18.      * @ORM\Id()
  19.      *
  20.      * @ORM\GeneratedValue()
  21.      *
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     private $id;
  25.     /**
  26.      * @ORM\Column(type="string", nullable=false, length=180)
  27.      */
  28.     private $label null;
  29.     public function getId(): ?int
  30.     {
  31.         return $this->id;
  32.     }
  33.     public function getLabel()
  34.     {
  35.         return $this->label;
  36.     }
  37.     public function setLabel($label)
  38.     {
  39.         $this->label $label;
  40.         return $this;
  41.     }
  42.     // Special case: Role
  43.     /**
  44.      * @ORM\Column(type="string", length=180, unique=true)
  45.      */
  46.     private $name;
  47.     public function getName(): ?string
  48.     {
  49.         return $this->name;
  50.     }
  51.     public function setName(string $name): self
  52.     {
  53.         $this->name $name;
  54.         return $this;
  55.     }
  56.     /**
  57.      * @ORM\Column(type="datetime", nullable=false)
  58.      */
  59.     private $created_at;
  60.     /**
  61.      * @ORM\Column(type="datetime", nullable=true)
  62.      */
  63.     private $updated_at null;
  64.     public function getCreatedAt(): \DateTimeInterface
  65.     {
  66.         return $this->created_at;
  67.     }
  68.     public function setCreatedAt(\DateTimeInterface $created_at): self
  69.     {
  70.         $this->created_at $created_at;
  71.         return $this;
  72.     }
  73.     public function getUpdatedAt(): ?\DateTimeInterface
  74.     {
  75.         return $this->updated_at;
  76.     }
  77.     public function setUpdatedAt(?\DateTimeInterface $updated_at): self
  78.     {
  79.         $this->updated_at $updated_at;
  80.         return $this;
  81.     }
  82.     /**
  83.      * @ORM\PrePersist
  84.      */
  85.     public function setCreatedAtValue()
  86.     {
  87.         $this->setCreatedAt(new \DateTime());
  88.     }
  89.     /**
  90.      * @ORM\PreUpdate
  91.      */
  92.     public function setUpdatedAtValue()
  93.     {
  94.         $this->setUpdatedAt(new \DateTime());
  95.     }
  96.     /**
  97.      * @ORM\Column(type="datetime", nullable=true)
  98.      */
  99.     private $deleted_at;
  100.     public function getDeletedAt(): ?\DateTimeInterface
  101.     {
  102.         return $this->deleted_at;
  103.     }
  104.     public function setDeletedAt(?\DateTimeInterface $deleted_at): self
  105.     {
  106.         $this->deleted_at $deleted_at;
  107.         return $this;
  108.     }
  109.     // Target of a LinkedEntity from RoleController (Controller)
  110.     // Origin of a OneToMany with User (Entity)
  111.     /**
  112.      * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="role")
  113.      */
  114.     private $users;
  115.     /**
  116.      * @ORM\Column(type="array", nullable=true)
  117.      */
  118.     private $privileges = [];
  119.     /**
  120.      * @return Collection|User[]
  121.      */
  122.     public function getUsers(): Collection
  123.     {
  124.         return $this->users;
  125.     }
  126.     public function addUser(User $user): self
  127.     {
  128.         if (!$this->users->contains($user)) {
  129.             $this->users[] = $user;
  130.             $user->setRole($this);
  131.         }
  132.         return $this;
  133.     }
  134.     public function removeUser(User $user): self
  135.     {
  136.         if ($this->users->contains($user)) {
  137.             $this->users->removeElement($user);
  138.             if ($user->getRole() === $this) {
  139.                 $user->setRole(null);
  140.             }
  141.         }
  142.         return $this;
  143.     }
  144.     public function getPrivileges(): ?array
  145.     {
  146.         if (!\is_array($this->privileges)) {
  147.             return [];
  148.         }
  149.         return array_values($this->privileges);
  150.     }
  151.     public function setPrivileges(?array $privileges): self
  152.     {
  153.         if (\is_array($privileges)) {
  154.             $this->privileges array_values($privileges);
  155.         } else {
  156.             $this->privileges $privileges;
  157.         }
  158.         return $this;
  159.     }
  160. }