Doctrine: rest methods

This commit is contained in:
Simon Vieille 2016-11-23 17:52:53 +01:00
parent ac36580272
commit 74d18b0514
3 changed files with 228 additions and 40 deletions

View file

@ -3,10 +3,9 @@
namespace AppBundle\Entity; namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** /**
* Author * Author.
* *
* @ORM\Table(name="author") * @ORM\Table(name="author")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AuthorRepository") * @ORM\Entity(repositoryClass="AppBundle\Repository\AuthorRepository")
@ -44,7 +43,15 @@ class Author
private $books; private $books;
/** /**
* Get id * Constructor.
*/
public function __construct()
{
$this->books = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
* *
* @return int * @return int
*/ */
@ -54,7 +61,7 @@ class Author
} }
/** /**
* Set firstname * Set firstname.
* *
* @param string $firstname * @param string $firstname
* *
@ -68,7 +75,7 @@ class Author
} }
/** /**
* Get firstname * Get firstname.
* *
* @return string * @return string
*/ */
@ -78,7 +85,7 @@ class Author
} }
/** /**
* Set lastname * Set lastname.
* *
* @param string $lastname * @param string $lastname
* *
@ -92,7 +99,7 @@ class Author
} }
/** /**
* Get lastname * Get lastname.
* *
* @return string * @return string
*/ */
@ -100,16 +107,9 @@ class Author
{ {
return $this->lastname; return $this->lastname;
} }
/**
* Constructor
*/
public function __construct()
{
$this->books = new \Doctrine\Common\Collections\ArrayCollection();
}
/** /**
* Add book * Add book.
* *
* @param \AppBundle\Entity\Book $book * @param \AppBundle\Entity\Book $book
* *
@ -123,7 +123,7 @@ class Author
} }
/** /**
* Remove book * Remove book.
* *
* @param \AppBundle\Entity\Book $book * @param \AppBundle\Entity\Book $book
*/ */
@ -133,7 +133,7 @@ class Author
} }
/** /**
* Get books * Get books.
* *
* @return \Doctrine\Common\Collections\Collection * @return \Doctrine\Common\Collections\Collection
*/ */
@ -141,4 +141,60 @@ class Author
{ {
return $this->books; return $this->books;
} }
/**
* 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 lastname.
*
* @JMS\Serializer\Annotation\SerializedName("lastname")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return string
*/
public function getRestLastname()
{
return $this->getLastname();
}
/**
* Rest: get firstname.
*
* @JMS\Serializer\Annotation\SerializedName("firstname")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return string
*/
public function getRestFirstname()
{
return $this->getFirstname();
}
/**
* 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();
}
} }

View file

@ -10,6 +10,7 @@ use Doctrine\Common\Collections\ArrayCollection;
* *
* @ORM\Table(name="book") * @ORM\Table(name="book")
* @ORM\Entity(repositoryClass="AppBundle\Repository\BookRepository") * @ORM\Entity(repositoryClass="AppBundle\Repository\BookRepository")
* @JMS\Serializer\Annotation\ExclusionPolicy("all")
*/ */
class Book class Book
{ {
@ -175,7 +176,7 @@ class Book
} }
/** /**
* Add category * Add category.
* *
* @param \AppBundle\Entity\Category $category * @param \AppBundle\Entity\Category $category
* *
@ -189,7 +190,7 @@ class Book
} }
/** /**
* Remove category * Remove category.
* *
* @param \AppBundle\Entity\Category $category * @param \AppBundle\Entity\Category $category
*/ */
@ -199,7 +200,7 @@ class Book
} }
/** /**
* Get categories * Get categories.
* *
* @return \Doctrine\Common\Collections\Collection * @return \Doctrine\Common\Collections\Collection
*/ */
@ -209,7 +210,7 @@ class Book
} }
/** /**
* Set author * Set author.
* *
* @param \AppBundle\Entity\Author $author * @param \AppBundle\Entity\Author $author
* *
@ -223,7 +224,7 @@ class Book
} }
/** /**
* Get author * Get author.
* *
* @return \AppBundle\Entity\Author * @return \AppBundle\Entity\Author
*/ */
@ -231,4 +232,92 @@ class Book
{ {
return $this->author; return $this->author;
} }
/**
* 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 title.
*
* @JMS\Serializer\Annotation\SerializedName("title")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return string
*/
public function getRestTitle()
{
return $this->getTitle();
}
/**
* Rest: get publishedAt.
*
* @JMS\Serializer\Annotation\SerializedName("publishedAt")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return string
*/
public function getRestPublishedAt()
{
if ($this->getPublishedAt()) {
return $this->getPublishedAt()->format('Y-m-d');
}
return null;
}
/**
* Rest: get pages.
*
* @JMS\Serializer\Annotation\SerializedName("pages")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return int
*/
public function getRestPages()
{
return $this->getPages();
}
/**
* Rest: get categories.
*
* @JMS\Serializer\Annotation\SerializedName("categories")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return ArrayCollection
*/
public function getRestCategories()
{
return $this->getCategories();
}
/**
* Rest: get author.
*
* @JMS\Serializer\Annotation\SerializedName("author")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return Author
*/
public function getRestAuthor()
{
return $this->getAuthor();
}
} }

View file

@ -3,13 +3,13 @@
namespace AppBundle\Entity; namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** /**
* Category * Category.
* *
* @ORM\Table(name="category") * @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository") * @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
* @JMS\Serializer\Annotation\ExclusionPolicy("all")
*/ */
class Category class Category
{ {
@ -41,7 +41,15 @@ class Category
private $books; private $books;
/** /**
* Get id * Constructor.
*/
public function __construct()
{
$this->books = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
* *
* @return int * @return int
*/ */
@ -51,7 +59,7 @@ class Category
} }
/** /**
* Set name * Set name.
* *
* @param string $name * @param string $name
* *
@ -65,7 +73,7 @@ class Category
} }
/** /**
* Get name * Get name.
* *
* @return string * @return string
*/ */
@ -73,16 +81,9 @@ class Category
{ {
return $this->name; return $this->name;
} }
/**
* Constructor
*/
public function __construct()
{
$this->books = new \Doctrine\Common\Collections\ArrayCollection();
}
/** /**
* Add book * Add book.
* *
* @param \AppBundle\Entity\Book $book * @param \AppBundle\Entity\Book $book
* *
@ -96,7 +97,17 @@ class Category
} }
/** /**
* Remove book * Get books.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBooks()
{
return $this->books;
}
/**
* Remove book.
* *
* @param \AppBundle\Entity\Book $book * @param \AppBundle\Entity\Book $book
*/ */
@ -106,12 +117,44 @@ class Category
} }
/** /**
* Get books * Rest: get id.
* *
* @return \Doctrine\Common\Collections\Collection * @JMS\Serializer\Annotation\SerializedName("id")
* @JMS\Serializer\Annotation\Groups({"all"})
* @JMS\Serializer\Annotation\VirtualProperty
*
* @return int
*/ */
public function getBooks() public function getRestId()
{ {
return $this->books; 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();
} }
} }