Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

ObjectMapper wrong association with child entity

已关闭
#7,801 3 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
php, symfony

调研方向

先从提供的 Product 和 Category entity/DTO 定义以及 Symfony ObjectMapper 配置开始,然后复现 Category 关系的已报告 GET 映射错误。当 Product DTO 的 category 能够从 Category entity 正确映射,且不再出现已报告的类型错误时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

State

API Platform version(s) affected: 4.2.14

Description

Hi !
When using ObjectMapper, child DTO management does not seem to work on a GET / GET Collection method.
When I have a Parent A entity that contains a child Aa and I have a DTO corresponding to these two entities, DtoA and DtoAa, then in my DtoA I am unable to map the child Dto.
Or perhaps I am missing something in my configuration or in my understanding of how to use ObjectMapper with ApiPlatform.
Maybe it's more related to ObjectMapper than ApiPlatform ?

How to reproduce

With this example, I get a mapping error on the category relationship. It seems that ObjectMapper is unable to connect to my resource.
Expected argument of type "?App\Api\ApiResource\Category", "App\Entity\Category" given at property path "category".

Entity Product :


declare(strict_types=1);

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Gedmo\Mapping\Annotation\Translatable;
use Symfony\Component\Validator\Constraints\NotBlank;
#[Entity]
class Product
{
    #[Id]
    #[GeneratedValue]
    #[Column(type: Types::INTEGER)]
    private ?int $id = null;

    #[ManyToOne(targetEntity: Category::class)]
    #[JoinColumn(nullable: true, onDelete: 'SET NULL')]
    private ?Category $category = null;

    #[Column(type: Types::STRING)]
    private string $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): void
    {
        $this->category = $category;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

Entity Category :

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

#[Entity]
class Category
{
    #[Id]
    #[GeneratedValue]
    #[Column(type: Types::INTEGER)]
    private ?int $id = null;

    #[Column(name: 'label', type: Types::STRING, nullable: false)]
    private ?string $label = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(?string $label): void
    {
        $this->label = $label;
    }
}

DTO Product :


declare(strict_types=1);

namespace App\Api\ApiResource;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Entity\Product as ProductEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/products/{id}',
        ),
    ],
    stateOptions: new Options(entityClass: ProductEntity::class)
)]
#[Map(source: ProductEntity::class)]
class Product
{
    public int $id;

    public string $name;

    public ?Category $category = null;
}

DTO Category

<?php

declare(strict_types=1);

namespace App\Api\ApiResource;

use App\Entity\Category as CategoryEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(source: CategoryEntity::class)]
class Category
{
    public int $id;
    public string $label;
}

Thank you for your help.

主要语言
PHP
星标
2.6k
派生
987
平均合并
1 天 10 小时
30 天内合并 PR
76

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

api-platform/core 的其他 Issue

查看 api-platform/core 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。