【发布时间】: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:migration 或php 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