api-platform.com - 具有有限属性的操作/GET
问题描述
当然已经问过了,但是...拥有这个实体
/**
* A form
*
* @ORM\Entity(repositoryClass=FormRepository::class)
*
* @ORM\Table(
* name="forms",
* options={"comment":"Table of the forms"},
* indexes={
* @ORM\Index(name="forms_idx_dofc", columns={"dateofcreation"}),
* @ORM\Index(name="forms_idx_dofu", columns={"dateofupdate"}),
* @ORM\Index(name="forms_idx_dofs", columns={"dateofsubmission"})
* }
* )
*
* @ApiResource(
* routePrefix="/",
* shortName="Forms",
* description="API Access : form Entity",
* collectionOperations={"GET"},
* itemOperations={"GET"},
* attributes={
* "normalization_context"={
* "groups"={
* "GET:FORM"
* }
* },
* "order"={
* "dateofsubmission",
* "dateofupdate",
* "dateofcreation"
* }
* }
* )
*
*/
class Form
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", options={"comment":"Primary Key, Auto generated"})
*/
private $id;
/**
* @var datetime Date of creation of the form.
*
* @ORM\Column(type="datetime", options={"comment":"date of creation of the form"})
*
* @Assert\NotBlank(message="The date time when was created the form should not be blank")
* @Assert\DateTime(message="The data time when was created the form should be the type DateTime")
*
* @Groups({"GET:FORM"})
*
* @ApiFilter(OrderFilter::class, strategy="ASC")
*/
private $dateofcreation;
/**
* @var datetime Date of update of the form.
*
* @ORM\Column(type="datetime", options={"comment":"date of update of the form"})
*
* @Assert\NotBlank(message="The date time when was updated the form should not be blank")
* @Assert\DateTime(message="The data time when was updated the form should be the type DateTime")
*
* @Groups({"GET:FORM"})
*
* @ApiFilter(OrderFilter::class, strategy="ASC")
*/
private $dateofupdate;
/**
* @var person Person that has created the form.
*
* @ORM\ManyToOne(targetEntity=Person::class, inversedBy="forms")
* @ORM\JoinColumn(nullable=false)
*
* @Groups({"GET:FORM"})
*
* @ApiFilter(SearchFilter::class, properties={"createdBy.id":"exact","createdBy.givenname":"ipartial", "createdBy.familyname":"ipartial"})
* @ApiFilter(OrderFilter::class, strategy="ASC")
*/
private $createdBy;
...
我希望有可能:一个基本的collectionOperations“GET”将返回所有具有@GROUP=GET:FORM的属性
备用 collectionOperations "GETDATES" 将返回具有 @GROUP=GET:DATES 的所有属性
(理想情况下)可以通过不同的路线访问操作。
GET => # http://api/forms?createdBy.id=25 => 所有属性
GETDATES => http://api/formsLimited?createdBy.id=25 => 只有日期
ps:我希望这对可读性更好。
解决方案
感谢 Grégoire Hébert(https://stackoverflow.com/users/4887997/gr%C3%A9goire-hebert)的支持(在 Slack 上)。解决方案非常简单。在 ApiResource() 中创建一个集合操作:
- 方法 = 获取
- 路径(将成为一条路线)
- 要应用于所需属性的组的规范化上下文
它给:
collectionOperations={"GET","GETLIMITED"={"method"="GET","path"="formslist","normalization_context"={"groups"="GET:LIMITED"}}},
推荐阅读
- python - 在 Django 应用程序中从服务器下载文件
- java - 优化代码以找到给定数 N 的阶乘的个位
- spring-boot - 可以将两个 camunda 实例连接到同一个数据库吗?
- python - pip install Flask 在我的环境中不起作用
- r - 将参数传递给R中网状结构的python函数
- apache-kafka - 使用cloudera进行Kafka通信
- assembly - ARM - 内存地址编号实际指向哪里 - 地址的顶部或底部?
- c - fifo linux - write() 函数突然终止程序
- typescript - 检查变量是否属于 Typescript 中的自定义类型
- python-3.x - 如何将 touchripple.py 与 Kivy lang 一起使用?