实体名称空间子类在Doctrine中不起作用

时间:2014-09-01 19:46:07

标签: php doctrine-orm namespaces doctrine

我正在使用Doctrine和Composer。

我在基类上有以下代码:

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

1 个答案:

答案 0 :(得分:0)

<强>解决

问题在于作曲家自动加载...

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

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

tnx为你的时间ppl。