[英]Entity namespace subclass not working in Doctrine
我在Composer中使用Doctrine。
我在基类上有以下代码:
Entidades\BaseTable.php...
<?php
use Doctrine\Common\Collections\ArrayCollection;
/**
* @MappedSuperclass
*/
class BaseTable
{
/**
* @Id @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @Column(type="datetime")
* @var datetime
*/
protected $criado_em;
public function __construct()
{
date_default_timezone_set('Australia/Melbourne');
$this->criado_em = new DateTime(null, new DateTimeZone('America/Sao_Paulo'));
}
public function getId()
{
return $this->id;
}
}
?>
在最后一堂课上,我得到了:
Entidades\Sistema\Aplicativo.php...
<?php
use Doctrine\Common\Collections\ArrayCollection;
namespace Entidades\Sistema;
/**
* @Entity @Table(name="sistema.aplicativos")
*/
class Aplicativo Extends \Entidades\BaseTable
{
/**
* @Column(type="string", nullable=false)
* @var string
*/
public $nome;
/**
* @Column(type="string", unique=true, nullable=false)
* @var string
*/
public $app_key;
/**
* @Column(type="string", unique=true, nullable=false)
* @var string
*/
public $esquema;
public function addAplicativo($nome,$esquema)
{
$this->nome = $nome;
$this->esquema = $esquema;
}
protected function newGuid()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
public function __construct()
{
parent::__construct();
$this->app_key = newGuid();
}
}
?>
当我在项目根目录上使用命令php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create
,它可以很好地进行创建。 但是当我做类似的事情时:
<?php
require_once "bootstrap.php";
$novo = new Sistema\Aplicativo();
$novo->nome = 'Teste';
$novo->esquema = 'Teste';
$entityManager->persist($novo);
$entityManager->flush();
echo "Aplicativo com o ID " . $product->getId() . " criado com sucesso.\n";
?>
PHP引发如下错误:
Fatal error: Class 'Entidades\BaseTable' not found in PROJECTPATH\Entidades\Sistema\Aplicativo.php on line 10
Call Stack
# Time Memory Function Location
1 0.0006 127464 {main}( ) ..\addAplicativo.php:0
2 0.0775 2090656 Composer\Autoload\ClassLoader->loadClass( ) ..\addAplicativo.php:0
3 0.0775 2090760 Composer\Autoload\includeFile( ) ..\ClassLoader.php:274
4 0.0782 2098408 include( 'D:\TRABALHO\Admin v3\Server\Entidades\Sistema\Aplicativo.php' ) ..\ClassLoader.php:382
解决了
问题是作曲家的自动加载...
我在这里犯了一个错误,解决方法是放入自动加载功能,将psd-4与名称空间一起使用,psr-0不能按预期工作...但是psr-4是...
"autoload": {
"psr-4": {
"": "Entidades/",
"Entidades\\": "Entidades/",
"Entidades\\Sistema\\": "Entidades/Sistema/"
}
}
tnx为您准备的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.