Php API平台:多个主键与招摇过市-获取错误
我正在尝试为具有多个主键的实体在没有自定义控制器的情况下向参数中添加第二个字段,并得到错误 现在一步一步: 数据库表:Php API平台:多个主键与招摇过市-获取错误,php,symfony,swagger,api-platform.com,Php,Symfony,Swagger,Api Platform.com,我正在尝试为具有多个主键的实体在没有自定义控制器的情况下向参数中添加第二个字段,并得到错误 现在一步一步: 数据库表: 创建表度量值类型 ( id varchar(10)不为空, 语言iso代码varchar(2)不为空, 标签varchar(255)null, 在datetime创建的\u不为空, 在datetime null更新了\u, 约束度量值类型id语言iso代码uindex 唯一(id、语言和iso代码), 约束度量\u类型\u语言\u iso\u代码\u fk 外键(语言iso代码
创建表度量值类型
(
id varchar(10)不为空,
语言iso代码varchar(2)不为空,
标签varchar(255)null,
在datetime创建的\u不为空,
在datetime null更新了\u,
约束度量值类型id语言iso代码uindex
唯一(id、语言和iso代码),
约束度量\u类型\u语言\u iso\u代码\u fk
外键(语言iso代码)引用语言(iso代码)
);
更改表度量值类型
添加主键(id、语言和iso代码);
src/Entity/MeasureType.php
namespace Api\Entity;
use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* @ORM\Table(
* name="measure_types",
* indexes={
* @ORM\Index(name="measure_types_uindex", columns={"id", "language_iso_code"})
* },
* uniqueConstraints={
* @ORM\UniqueConstraint(name="measure_types_uindex", columns={"id", "language_iso_code"})
* }
* )
* @ORM\Entity(repositoryClass="Klarstein\Api\Repository\MeasureTypeRepository")
* @ORM\HasLifecycleCallbacks
*/
class MeasureType
{
/**
* @ORM\Id()
* @ORM\Column(type="string", length=10)
*/
private string $id;
/**
* @ORM\Id()
* @ORM\ManyToOne(targetEntity="Language", fetch="EAGER")
* @ORM\JoinColumn(name="language_iso_code", referencedColumnName="iso_code")
*/
private Language $language;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private string $label;
/**
* @ORM\Column(type="datetime")
*/
private DateTimeInterface $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTimeInterface $updatedAt;
public function __construct()
{
$this->createdAt = new DateTime();
}
/**
* @ORM\PreUpdate
*
* @throws Exception
*/
public function onPutHandler(): void
{
$this->updatedAt = new DateTime();
}
// ... under this comment getters and setters
}
config/api/measure_type.yaml
resources:
Api\Entity\MeasureType:
properties:
id:
identifier: true
language:
identifier: true
attributes:
pagination_items_per_page: 25
collectionOperations:
get:
normalization_context:
groups: ['v1_get_collection']
post:
normalization_context:
groups: ['v1_post_collection_response']
denormalization_context:
groups: ['v1_post_collection_request']
itemOperations:
get:
method: 'GET'
normalization_context:
groups: ['v1_get_item']
put:
normalization_context:
groups: ['v1_put_item_response']
denormalization_context:
groups: ['v1_put_item_request']
delete:
denormalization_context:
groups: ['v1_delete_item']
/measure_types/id={id};language={language}:
get:
id:
name: 'id'
description: 'Measure type ID'
in: 'path'
required: true
type: 'string'
example: 'ml'
language:
name: 'language'
description: 'Language ISO code'
in: 'path'
required: true
type: 'string'
example: 'de'
这是目前的工作:
但是我想在doc中显示,我希望有两个参数作为标识符
我在做什么:
添加:path:'/measure\u types/id={id};language={language}'
toconfig/api/measure\u type.yaml
resources:
Api\Entity\MeasureType:
properties:
id:
identifier: true
language:
identifier: true
attributes:
pagination_items_per_page: 25
collectionOperations:
get:
normalization_context:
groups: ['v1_get_collection']
post:
normalization_context:
groups: ['v1_post_collection_response']
denormalization_context:
groups: ['v1_post_collection_request']
itemOperations:
get:
method: 'GET'
path: '/measure_types/id={id};language={language}'
normalization_context:
groups: ['v1_get_item']
put:
normalization_context:
groups: ['v1_put_item_response']
denormalization_context:
groups: ['v1_put_item_request']
delete:
denormalization_context:
groups: ['v1_delete_item']
下一步:我有SwaggerEventRequire
Decorator:
src/Swagger/SwaggerEventRequireDecorator.php
namespace Api\Swagger;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Yaml\Yaml;
final class SwaggerEventRequireDecorator implements NormalizerInterface
{
private const SWAGGER_DECORATIONS = __DIR__ . '/../../config/swagger/';
private NormalizerInterface $decorated;
public function __construct(NormalizerInterface $decorated)
{
$this->decorated = $decorated;
}
public function normalize($object, string $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
$customDefinition = $this->loadDefinitions();
foreach ($customDefinition as $path => $methods) {
foreach ($methods as $method => $parameters) {
foreach ($parameters as $paramKey => $paramValues) {
if (empty($paramValues['name'])) {
continue;
}
if (empty($docs['paths'][$path]) || empty($docs['paths'][$path][$method])) {
continue;
}
// e.g. remove an existing event parameter
$docs['paths'][$path][$method]['parameters'] = array_values(
array_filter(
$docs['paths'][$path][$method]['parameters'],
function ($param) use ($paramKey) {
return $param['name'] !== $paramKey;
}
)
);
// e.g. add the new definition for event
$docs['paths'][$path][$method]['parameters'][] = $paramValues;
}
}
}
return $docs;
}
public function supportsNormalization($data, string $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
private function loadDefinitions(): array
{
$result = [];
$finder = new Finder();
$finder
->files()
->in(self::SWAGGER_DECORATIONS)
->name('*.yaml');
foreach ($finder as $file) {
$yaml = Yaml::parseFile(self::SWAGGER_DECORATIONS . $file->getFilename());
if (empty($yaml)) {
continue;
}
$result = array_unique(array_merge($result, $yaml), SORT_REGULAR);
}
return $result;
}
}
和装饰配置文件:config/swagger/measure_type.yaml
resources:
Api\Entity\MeasureType:
properties:
id:
identifier: true
language:
identifier: true
attributes:
pagination_items_per_page: 25
collectionOperations:
get:
normalization_context:
groups: ['v1_get_collection']
post:
normalization_context:
groups: ['v1_post_collection_response']
denormalization_context:
groups: ['v1_post_collection_request']
itemOperations:
get:
method: 'GET'
normalization_context:
groups: ['v1_get_item']
put:
normalization_context:
groups: ['v1_put_item_response']
denormalization_context:
groups: ['v1_put_item_request']
delete:
denormalization_context:
groups: ['v1_delete_item']
/measure_types/id={id};language={language}:
get:
id:
name: 'id'
description: 'Measure type ID'
in: 'path'
required: true
type: 'string'
example: 'ml'
language:
name: 'language'
description: 'Language ISO code'
in: 'path'
required: true
type: 'string'
example: 'de'
结果,我在doc中得到了工作表单,但结果错误:
我希望在没有自定义控制器的情况下使用它,因为我有许多具有多个主键的实体,为每个主键添加控制器将花费大量时间
我做错了什么?解决了。解决方案已打开。已解决。解决办法就在凯文身上