Symfony/Api-Platform - 如何为 ManyToMany 关系的 addItem/removeItem 函数公开 CRUD 操作?
我有一个 Angular Web 应用程序,它向 Symfony/Api 平台 API 的公开 CRUD 端点发送请求。基本功能是用户可以收藏项目。
所以关系是:许多用户可能喜欢许多项目。 我想通过一个请求添加(例如最喜欢的)或删除(例如不喜欢的)一个或多个项目,但这似乎相当复杂。
目前,我只能通过发送带有如下正文的 PUT 请求来更新 Items-User 数组:
{
items: [iri1, iri2]
}
现在,如果我想添加或删除 >Item 整个集合需要重新发送:
{
items: [iri1, iri2, iri3]
}
Symfony/Api-Platform 不允许直接 CRUD 公开添加/删除功能,但我正在寻找一种仍然公开该功能的方法,因为它似乎有点低效重置整个集合,尤其是数额巨大。
现在我有两个问题:
1:Doctrine 是否足够聪明,能够知道从传递的请求 items-property 中添加或删除了哪个 Item ?据我所知,Doctrine 通过调用 setElements() 来“更新”ArrayCollection,覆盖整个集合。但也许我丢失了一些东西,并且只会调用附加/丢失项目的添加/删除功能。重新设置已经保存到数据库的 Items 是相当多余的...
2:如何实现一种从数据库中有效添加/删除 Item 的方法用户项目属性,最好通过与实体相关的东西,例如自定义DataPersister,或者如果可能的话,通过注释。
I have an Angular Web-App that sends requests to exposed CRUD-Endpoints of a Symfony/Api-Platform API. The basic functionality is that a User can favorite Items.
So the relationship is: Many Users may favorite many Items.
I want to add (e.g. favorite) or remove (e.g. un-favorite) just one or more Items with a single request, but that seems quite complicated.
Currently I can only update the Items-Array of User by sending a PUT-request with a body that looks like this:
{
items: [iri1, iri2]
}
Now If I want to add or delete an Item the whole collection needs to be resent:
{
items: [iri1, iri2, iri3]
}
Symfony/Api-Platform does not allow direct CRUD exposure of add/remove functions, but I am looking for a way to still expose that functionality, because it appears somewhat inefficient to reset the whole collection, especially with huge amounts.
Now I have 2 questions:
1: Is Doctrine smart enough to know which Item has been added or removed from the passed request items-property? As far as I know Doctrine "updates" an ArrayCollection by calling setElements(), overriding the whole set. But maybe I am missing stuff and only the add/remove functions for additional/missing Items will be called. It is quite redundant to re-set Items that have already persisted to the database...
2: How do I implement a way to efficiently add/remove an Item from the User items-property, preferably through something Entity-Related e.g. a custom DataPersister or, if possible, Annotations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论