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

Symfony中App\Entity\UserGroup类名重复致命错误求助

Symfony 类重复声明错误解决办法

Fatal error: Cannot declare class App\Entity\UserGroup, because the name is already in use in /var/www/app/src/Entity/UserGroup.php on line 17

你已经清空缓存、重装依赖但问题依旧,结合你的代码和配置,问题出在以下几个地方:

1. 注释语法错误

你的类和属性注释存在语法错误:

  • 类上方的注释用/*开头,但结尾写成了/,正确收尾应为*/
  • 属性的注释开头写成了/,正确开头应为/*

2. 重复的Doctrine实体注解

类上重复声明了@ORM\Entity注解,一个不带仓库类,一个带仓库类,这会导致Doctrine处理注解时冲突。

修复后的代码示例

<?php

namespace App\Entity;

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

/**
 * @ORM\Table(name="UserGroup", indexes={@ORM\Index(name="usergroup_license_id_foreign", columns={"license_id"})})
 * @ORM\Entity(repositoryClass="App\Repository\UsergroupRepository")
 */
class UserGroup{
    /**
     * @var int
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id 
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */ 
    private $id;
}

额外检查步骤

  • 确认src/Entity/目录下没有其他同名文件(比如大小写不同的usergroup.php,Linux系统对文件名大小写敏感)
  • 执行以下命令确保自动加载和缓存正常:
    composer dump-autoload
    php bin/console cache:clear --env=prod
    php bin/console cache:clear --env=dev
    

内容的提问来源于stack exchange,提问作者Hicham Dev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 12:50:15