Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

ObjectMapper wrong association with child entity

クローズ
#7,801 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
76

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

api-platform/core のほかの issue

api-platform/core の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。