基于DTO创建了一个非常简单的资源。所有的逻辑都将通过自定义的DataProvider
提供,对于这个项目,没有使用注释。
资源的配置为:
<resources xmlns="https://api-platform.com/schema/metadata">
<resource class="AppDomainDtoProductUpgradePrice">
<collectionOperations>
<collectionOperation name="get"/>
</collectionOperations>
<itemOperations>
<itemOperation name="get"/>
</itemOperations>
</resource>
</resources>
实际DTO为:
<?php declare(strict_types=1);
namespace AppDomainDto;
/** @psalm-immutable */
class ProductUpgradePrice
{
public function __construct(
public string $productId,
public int $regularPrice,
public int $upgradePrice
) {}
}
当我尝试GET /api/product_upgrade_prices
时,我得到:
没有为AppDomainDtoProductUpgradePrice类型的资源定义标识符
有意义。
文档提到使用注释@ApiProperty
,但正如我前面提到的,这个项目没有注释。(我提交了一份PR来更新文档,因为我发布了这个问题,它被合并了,所以希望未来的用户不会遇到同样的情况)
我已经尝试将此添加到资源配置:
<property name="productId">
<attribute name="identifier">true</attribute>
</property>
却得到了相同的结果。
我怎么能配置这个不使用注释?
查看元数据XSD,在第113行可以看到property
元素的这个属性:
<xsd:attribute type="xsd:boolean" name="identifier"/>
这意味着你应该用这个代替:
<property identifier="true" name="id" />