gpt4 book ai didi

php - 实体命名空间子类在 Doctrine 中不起作用

转载 作者:行者123 更新时间:2023-12-04 16:52:14 25 4
gpt4 key购买 nike

我在 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

最佳答案

已解决

问题在于 Composer 自动加载...

我在那里犯了一个错误,解决方案是放入自动加载,带有命名空间的 psd-4,psr-0 没有按预期工作......但是 psr-4 是的......

"autoload": {
"psr-4": {
"": "Entidades/",
"Entidades\\": "Entidades/",
"Entidades\\Sistema\\": "Entidades/Sistema/"
}
}

tnx 为你的时间 ppl。

关于php - 实体命名空间子类在 Doctrine 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com