ManyToMany relations
I am starting to use api-platform core bundle and running into some problems that I can't seem to get working or maybe I am just doing wrong stuff. What I am trying to do is to get ManyToMany relations between two tables. For example this exemple, I'll make a manytomany between a user and a book.
User entity
/**
* @ApiResource(attributes={"normalization_context"={"groups"={"book"}}})
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Groups({"book"})
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Book", cascade={"persist"}, inversedBy="users"')
*/
private $books;
public function addBook(Book $book)
{
$this->books[] = $book;
}
public function removeBook(Book $book)
{
$this->books->removeElement($book);
}
/**
* return ArrayCollection
*/
public function getBooks()
{
return $this->books;
}
public function __construct()
{
parent::__construct();
$this->books = new ArrayCollection();
}
Book Entity
/**
* Book
* @ApiResource(attributes={"normalization_context"={"groups"={"book"}}})
* @ORM\Table(name="book")
* @ORM\Entity()
*/
class Book
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Groups({"book"})
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var User[]
* @ORM\ManyToMany(targetEntity="User", mappedBy="books")
*/
private $users;
public function __construct()
{
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Book
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add user
*
* @param \AppBundle\Entity\User $user
*
* @return Book
*/
public function addUser(\AppBundle\Entity\User $user)
{
$this->users[] = $user;
return $this;
}
/**
* Remove user
*
* @param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
$this->users->removeElement($user);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
What I would expect if i go to http://localhost:8000/api/ is to see all my routes for user and book, this is working ok. But I would like to also see those kinds of routes: /users/{id}/books/{id} and/or /books/{id}/users/{id}
How can I make 'subroutes' work ? Am I doing everything correctly ? Thank you for your help :)
In orders to makes subroutes you could use custom operation.
But why would you need that ? you can embbed documents if you need to have the all the books related to the user and so on. You should set a depth limit too.
Thanks for your help, I have looked at the embbed document option and this is working quite nicely, however I have two more questions :
-
First, if I get a user on route /api/users/1 , I will get all the related books which is fine. If I then need to add a book, I can't directly update /api/users/1 with {books: ["api/books/10"]} because it will then remove the previous books if there was any. I have to retrieve all current books linked to the user and send then back again with the new one, so something like {book:["api/books/9","api/books/10"]} is that correct ? I would like to do an update directly on /api/users/1/books , but is that the best way ?
-
Pretty much the same question to remove one book, do I need to retrieve all books linked to the user and resend the books without the URI of the one I want to remove ? wouldn't it be possible to do a DELETE on /api/users/1/books/1 for example ?
Thanks again for your help and please let me know if I am going the wrong path ...
1/ What you can do is to post a news books with a user related directly to /api/books but if you want to add it by the users, yes you have to resend all the books
2/ pretty much the same as 1, but with DELETE method ;)
@Simperfit In my case I do not want to see books related to user when calling /api/users/{id}, but I do want to see all books related to user when calling /api/users/{id}/books. What would be the best way to go?
IMO you could use a custom operation to make that happen.
Or a filter in the books entrypoint, something like /api/books?users=ID that will return all the books related to that users will do the job too.
Yes, I made it work, but I had to write it like /api/books?users.id=ID and I am going to have a fake CustomOperation "/api/users/{user_id}/books" which will make a subrequest to "/api/books?users.id={user_id}". But it seems hacky, isn't it?
I wonder if a custom CollectionDataProvider is a better place to do that?
It seems hacky, I agree.
Yes you could totally do that in a CollectionDataProvider ;)
@samusenkoiv Is it ok for you ?
Yeah, I went with CollectionDataProvider, you can close the issue) anyway Im looking forward to seeing https://github.com/api-platform/core/pull/904A to be released
@samusenkoiv it has! :D https://dunglas.fr/2017/06/api-platform-2-1-feature-walkthrough-create-blazing-fast-hypermedia-apis-generate-js-apps/
@mllobere @ihorsamusenko @soyuka I am stuck on how do I PATCH ManyToMany relation without actually fetching all of the already related resource. So, basically api to just PATCH newly added relations and delete deleted relations. If you can guide me in this, it would be grateful.