sf-api-example/src/AppBundle/Entity/Category.php

170 lines
3.3 KiB
PHP
Raw Normal View History

2016-11-23 16:10:52 +01:00
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
2016-11-23 17:52:53 +01:00
* Category.
2016-11-23 16:10:52 +01:00
*
2016-11-24 00:12:34 +01:00
* 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.
*
2016-11-23 16:10:52 +01:00
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
2016-11-23 17:52:53 +01:00
* @JMS\Serializer\Annotation\ExclusionPolicy("all")
2016-11-23 16:10:52 +01:00
*/
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
*
2016-11-23 20:10:46 +01:00
* @ORM\ManyToMany(
2016-11-23 16:10:52 +01:00
* targetEntity="Book",
* mappedBy="categories",
* cascade={"persist"}
* )
*/
private $books;
/**
2016-11-23 17:52:53 +01:00
* Constructor.
*/
public function __construct()
{
$this->books = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
2016-11-23 16:10:52 +01:00
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
2016-11-23 17:52:53 +01:00
* Set name.
2016-11-23 16:10:52 +01:00
*
* @param string $name
*
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
2016-11-23 17:52:53 +01:00
* Get name.
2016-11-23 16:10:52 +01:00
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
2016-11-23 17:52:53 +01:00
* Add book.
2016-11-23 16:10:52 +01:00
*
* @param \AppBundle\Entity\Book $book
*
* @return Category
*/
public function addBook(\AppBundle\Entity\Book $book)
{
$this->books[] = $book;
return $this;
}
/**
2016-11-23 17:52:53 +01:00
* Get books.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBooks()
{
return $this->books;
}
/**
* Remove book.
2016-11-23 16:10:52 +01:00
*
* @param \AppBundle\Entity\Book $book
*/
public function removeBook(\AppBundle\Entity\Book $book)
{
$this->books->removeElement($book);
}
/**
2016-11-23 17:52:53 +01:00
* Rest: get id.
2016-11-23 16:10:52 +01:00
*
2016-11-23 17:52:53 +01:00
* @JMS\Serializer\Annotation\SerializedName("id")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return int
2016-11-23 16:10:52 +01:00
*/
2016-11-23 17:52:53 +01:00
public function getRestId()
2016-11-23 16:10:52 +01:00
{
2016-11-23 17:52:53 +01:00
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();
2016-11-23 16:10:52 +01:00
}
}