首页 > 解决方案 > 具有有限属性的操作/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:我希望这对可读性更好。

标签: api-platform.com

解决方案


感谢 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"}}},

推荐阅读