错误:“ Autoloader预期的类...错字错误”,但是在symfony 4类继承中没有错字

时间:2019-02-01 15:00:28

标签: php symfony class inheritance

我有一个类文件“ 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/"
    }
},

2 个答案:

答案 0 :(得分:0)

在没有use语句的情况下,PHP自动加载器在同一目录中搜索该Documento类以对其进行加载。

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

答案 1 :(得分:0)

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

从那里,我运行了php bin/console make:migration命令和php bin/console doctrine:migrations:migrate,但是src / Migrations内部生成的迁移文件具有另一个不同于“ App”的命名空间,因此我不得不向服务中的异常添加“ Migrations”文件夹。 yaml,因此它反映了这一变化,并且在查找类时忽略了该文件夹:

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

然后,没有错误☺

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