You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony5双向类关联映射错误:自关联分类实体配置报错

问题修复方案

以下是你代码中存在的错误及修正后的完整实现:

核心错误点

  • ManyToOne关联的JoinColumn配置错误:原配置中外键列名误写为主键列id_category,关联的目标主键字段名误写为不存在的id,且未配置nullable=true允许父级为空,和你需求不匹配。
  • 存在冗余方法:getCategories、addCategory、removeCategory三个方法操作的是未定义的$categories属性,没有对应的实体字段,直接删除即可。
  • 索引配置的外键列名需要和关联列名保持一致:你索引中定义的外键列为id_cat_parent,要和JoinColumn的name参数对应。

修正后代码

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Category
 *
 * @ORM\Table(name="category", indexes={@ORM\Index(name="id_category_1", columns={"id_cat_parent"})})
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @var int
     *
     * @ORM\Column(name="id_category", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idCategory;

    /**
     * @var string
     *
     * @ORM\Column(name="denomination", type="string", length=50, nullable=false)
     */
    private $denomination;

    /**
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="childrenCategories", fetch="EAGER")
     * @ORM\JoinColumn(name="id_cat_parent", referencedColumnName="id_category", nullable=true)
     */
    private $categoryParent;

    /**
     * @ORM\OneToMany(targetEntity=Category::class, mappedBy="categoryParent", fetch="EAGER")
     */
    private $childrenCategories;

    public function __construct()
    {
        $this->childrenCategories = new ArrayCollection();
    }

    public function getIdCategory(): ?int
    {
        return $this->idCategory;
    }

    public function getDenomination(): ?string
    {
        return $this->denomination;
    }

    public function setDenomination(string $denomination): self
    {
        $this->denomination = $denomination;

        return $this;
    }

    public function getCategoryParent(): ?self
    {
        return $this->categoryParent;
    }

    public function setCategoryParent(?self $categoryParent): self
    {
        $this->categoryParent = $categoryParent;

        return $this;
    }

    /**
     * @return Collection|Category[]
     */
    public function getChildrenCategories(): Collection
    {
        return $this->childrenCategories;
    }

    public function addChildrenCategory(Category $childrenCategory): self
    {
        if (!$this->childrenCategories->contains($childrenCategory)) {
            $this->childrenCategories[] = $childrenCategory;
            $childrenCategory->setCategoryParent($this);
        }

        return $this;
    }

    public function removeChildrenCategory(Category $childrenCategory): self
    {
        if ($this->childrenCategories->removeElement($childrenCategory)) {
            if ($childrenCategory->getCategoryParent() === $this) {
                $childrenCategory->setCategoryParent(null);
            }
        }

        return $this;
    }
}

可选优化建议

如果你的分类层级超过2级,不建议使用fetch="EAGER",会触发多层级关联查询导致性能下降,需要获取子分类时再调用getChildrenCategories方法触发懒加载即可。

内容的提问来源于stack exchange,提问作者Fouvet Sébastien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 12:36:03