Doctrine2 using namespace error Composer

Asked

Viewed 71 times

2

Hello I am creating a file that serves as base class in Basetable.php Entities as follows:

<?php namespace Entidades;
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;
    }
}
?>

The other file is a base extend, which is in Application System Entities.php as follows:

<?php namespace Entidades\Sistema;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @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();
    }
}
?>

When running the command "php vendor/Doctrine/Orm/bin/Doctrine Orm:schema-tool:create" by the command everything works right, without problems, tables are created and etc...

But when running a php as the example below:

<?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";
?>

I get the following error in PHP:

Fatal error: Class 'Entidades\BaseTable' not found in APPPATH\Entidades\Sistema\Aplicativo.php on line 9
Call Stack
#   Time    Memory  Function    Location
1   0.0005  127704  {main}( )   ..\addAplicativo.php:0
2   0.0625  2090808 Composer\Autoload\ClassLoader->loadClass( ) ..\addAplicativo.php:0
3   0.0625  2090912 Composer\Autoload\includeFile( )    ..\ClassLoader.php:274
4   0.0632  2098568 include( 'D:\TRABALHO\Admin v3\Server\Entidades\Sistema\Aplicativo.php' )   ..\ClassLoader.php:382

As you can see the.php App file is included, I can find it without problems, the problem is to get its extend... I do not know why this is happening, but I can not correct nor by prayer, I looked for some similar examples on the Internet and for help in several sites, but nothing that relates to this, I believe I’m doing some nonsense grotesque... grateful for the attention.

  • Have you ever tried to put the two entities in the same namespace ?

  • Same error... I tried putting the class in the same folder and in the same namespace, in different files of course... Gives the same error... It is as if the autoloader failed to load the Basetable.php file

  • You could put your Composer.json file on ask so I can help you out ?

1 answer

2

SOLVED

The problem was with Autopload...

I made a silly mistake, the solution was to put psr-4 instead of psr-0 in the autoload of Poser, after changing psr-0 to psr-4 worked all 100%m follows the solution...

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

Grateful for the personal attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.