【问题标题】:Error: "Autoloader expected class... typo error" but no typo in symfony 4 class inheritance错误:“自动加载器预期的类...拼写错误”但在 symfony 4 类继承中没有拼写错误
【发布时间】:2019-06-26 04:17:39
【问题描述】:

我有一个类文件“Ajuste.php”:

<? // src/App/Entity/Ajuste.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\SuperClass\Documento as Documento;
use Doctrine\Common\Collections\ArrayCollection;

/** @ORM\Entity */
class Ajuste extends Documento
{
    /** 
     * @Id 
     * @Column(type="integer") 
     * @ORM\GeneratedValue(strategy="AUTO")
     * */
    private $id;

    /** @ORM\Column(type="string") */
    private $name;

...more fields and methods
}

超类“Documento.php”位于另一个文件夹中:

<? // src/SuperClass/Documento.php

namespace App\SuperClass;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperclass 
 * */
class Documento
{
    /** @ORM\Column(type="DateTime") */
    protected $fecha_emision;

    /** @ORM\Column(type="DateTime") */
    protected $fecha_registro;

    ...more fields and methods
}

services.yaml 是:

[...]
services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your                         services.
        autoconfigure: true # Automatically registers your services as     commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Admin,Resources,SuperClass}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
[...]

当我尝试php bin/console make:migrationphp bin/console doctrine:schema:validate 时,问题就开始了。 错误是:

In DebugClassLoader.php line 204:

The autoloader expected class "App\SuperClass\Documento" to be defined in file "[more dirs]../src/SuperClass/Documento.php". The file was found but the class was not in it, the class name or namespace probably has a typo.`

事实是我无法让它工作,即使我从服务的排除列表中删除“SuperClass”目录,错误仍然会出现(在这种情况下,错误仍然存​​在并且还指出了一个错字services.yaml),但我认为这应该与 symfony 4 的变化有关,似乎我无法弄清楚。提前谢谢你。

编辑: 目录结构截图,以防万一。 (https://imgur.com/a/PNnn3mR)

编辑: 添加 Composer 自动加载部分:

"autoload": {
    "psr-4": {
        "App\\": "src/"
    }
},
"autoload-dev": {
    "psr-4": {
        "App\\Tests\\": "tests/"
    }
},

【问题讨论】:

  • src/App/Entity/Ajuste.php中Documento的use语句在哪里?
  • 抱歉,补充一下,它从一开始就在那里。还是一样的错误。
  • PS:为了它的价值,一般来说最好在你的代码中使用英文(类,字段名等)。它是通用技术语言,每个人都会更容易理解您的代码。混合使用英语和西班牙语会使代码更难阅读。
  • 只是为了笑,确保您正在查看的文件与应用程序正在使用的文件相同。可能听起来很奇怪,但并不像您想象的那样在一个目录中工作而应用程序实际上在另一个目录中。
  • 请显示您的 composer.json,自动加载器部分。

标签: php symfony class inheritance


【解决方案1】:

我已经删除了这两个类,然后我使用php bin/console make:entity 命令再次创建了它们,并且没有错误。 ORM 注释中似乎存在某种错误。

从那里,我运行了php bin/console make:migration 命令和php bin/console doctrine:migrations:migrate,但是在 src/Migrations 中生成的迁移文件有另一个命名空间而不是“App”,所以我必须将“Migrations”文件夹添加到 services.yaml 中的异常中,以便它反映在查找类时更改并省略文件夹:

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Admin,Resources,SuperClass,Migrations}'

然后,没有错误☺

我只是想指出这一点,以防有人遇到这个问题。

【讨论】:

    【解决方案2】:

    如果没有 use 语句,PHP 自动加载器会在同一目录中搜索 Documento 类来加载它。

    src/App/Entity/Ajuste.php 中添加use App\SuperClass\Documento 应该可以解决您的问题

    【讨论】:

    • 对不起,我编辑了问题,那行从一开始就在那里,但忘记添加了。仍然,同样的错误。
    • 您是否正确检查了文件名?它们真的是在同一个案例中给出的吗?
    • 是的,对这些名称进行了三次检查,但这里是文件名和目录结构的屏幕截图 (imgur.com/a/PNnn3mR),以防万一。
    猜你喜欢
    • 2020-12-20
    • 2011-07-21
    • 2018-06-15
    • 1970-01-01
    • 2020-08-25
    • 2022-12-07
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多