170 lines
3.3 KiB
PHP
170 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace AppBundle\Entity;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
/**
|
|
* Category.
|
|
*
|
|
* Tous les objets retournés dans l'API sont transformés
|
|
* de manière à pouvoir être retournés au format JSON ou XML.
|
|
* Le comportement par défaut est de récupérer l'ensemble des attributs
|
|
* et de les retourner tels quels. Je prend le parti de choisir
|
|
* les données à afficher via les annotations JMS et les méthodes
|
|
* que je préfix par `getRest`. Ça permet donc de filtrer
|
|
* ce que l'on veut voir apparître dans l'API et ce qui ne doit pas y
|
|
* figurer.
|
|
*
|
|
* @ORM\Table(name="category")
|
|
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
|
|
* @JMS\Serializer\Annotation\ExclusionPolicy("all")
|
|
*/
|
|
class Category
|
|
{
|
|
/**
|
|
* @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 \Doctrine\Common\Collections\Collection
|
|
*
|
|
* @ORM\ManyToMany(
|
|
* targetEntity="Book",
|
|
* mappedBy="categories",
|
|
* cascade={"persist"}
|
|
* )
|
|
*/
|
|
private $books;
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->books = new \Doctrine\Common\Collections\ArrayCollection();
|
|
}
|
|
|
|
/**
|
|
* Get id.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* Set name.
|
|
*
|
|
* @param string $name
|
|
*
|
|
* @return Category
|
|
*/
|
|
public function setName($name)
|
|
{
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get name.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* Add book.
|
|
*
|
|
* @param \AppBundle\Entity\Book $book
|
|
*
|
|
* @return Category
|
|
*/
|
|
public function addBook(\AppBundle\Entity\Book $book)
|
|
{
|
|
$this->books[] = $book;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get books.
|
|
*
|
|
* @return \Doctrine\Common\Collections\Collection
|
|
*/
|
|
public function getBooks()
|
|
{
|
|
return $this->books;
|
|
}
|
|
|
|
/**
|
|
* Remove book.
|
|
*
|
|
* @param \AppBundle\Entity\Book $book
|
|
*/
|
|
public function removeBook(\AppBundle\Entity\Book $book)
|
|
{
|
|
$this->books->removeElement($book);
|
|
}
|
|
|
|
/**
|
|
* Rest: get id.
|
|
*
|
|
* @JMS\Serializer\Annotation\SerializedName("id")
|
|
* @JMS\Serializer\Annotation\Groups({"all"})
|
|
* @JMS\Serializer\Annotation\VirtualProperty
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getRestId()
|
|
{
|
|
return $this->getId();
|
|
}
|
|
|
|
/**
|
|
* Rest: get name.
|
|
*
|
|
* @JMS\Serializer\Annotation\SerializedName("name")
|
|
* @JMS\Serializer\Annotation\Groups({"all"})
|
|
* @JMS\Serializer\Annotation\VirtualProperty
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getRestName()
|
|
{
|
|
return $this->getName();
|
|
}
|
|
|
|
/**
|
|
* Rest: get books.
|
|
*
|
|
* @JMS\Serializer\Annotation\SerializedName("books")
|
|
* @JMS\Serializer\Annotation\Groups({"all"})
|
|
* @JMS\Serializer\Annotation\VirtualProperty
|
|
*
|
|
* @return ArrayCollection
|
|
*/
|
|
public function getRestBooks()
|
|
{
|
|
return $this->getBooks();
|
|
}
|
|
}
|