2013-02-26 17 views
27

ottengo un'eccezione Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Users" is not a valid entity or mapped super class ogni volta quando si esegue il codice successivo:Doctrine2 - "classe" non è un'entità valido o mappato super classe

test.php

<?php 
require_once "vendor/autoload.php"; 

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 

$paths = array(dirname(__FILE__)."/entities"); 
$isDevMode = false; 

// the connection configuration 
$dbParams = array(
    'driver' => 'pdo_mysql', 
    'user'  => 'root', 
    'password' => 'pass', 
    'dbname' => 'snabcentr', 
); 

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); 
$em = EntityManager::create($dbParams, $config); 

$user = $em->find("Users", 5); 

entità/Utenti .php

<?php 
use Doctrine\ORM\Mapping as ORM; 

/** 
* Users 
* 
* @ORM\Table(name="users") 
* @ORM\Entity 
*/ 
class Users 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="email", type="string", length=255, nullable=true) 
    */ 
    private $email; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="password", type="string", length=255, nullable=true) 
    */ 
    private $password; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="type", type="string", nullable=true) 
    */ 
    private $type; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_inn", type="string", length=255, nullable=true) 
    */ 
    private $clientInn; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_ogrn", type="string", length=255, nullable=true) 
    */ 
    private $clientOgrn; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_rs", type="string", length=255, nullable=true) 
    */ 
    private $clientRs; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_ks", type="string", length=255, nullable=true) 
    */ 
    private $clientKs; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_bik", type="string", length=255, nullable=true) 
    */ 
    private $clientBik; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_uaddress", type="string", length=255, nullable=true) 
    */ 
    private $clientUaddress; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_faddress", type="string", length=255, nullable=true) 
    */ 
    private $clientFaddress; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="client_daddress", type="string", length=255, nullable=true) 
    */ 
    private $clientDaddress; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=true) 
    */ 
    private $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="notes", type="text", nullable=true) 
    */ 
    private $notes; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="added_date", type="datetime", nullable=true) 
    */ 
    private $addedDate; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set email 
    * 
    * @param string $email 
    * @return Users 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    /** 
    * Get email 
    * 
    * @return string 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set password 
    * 
    * @param string $password 
    * @return SnabUsers 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    /** 
    * Get password 
    * 
    * @return string 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Set type 
    * 
    * @param string $type 
    * @return SnabUsers 
    */ 
    public function setType($type) 
    { 
     $this->type = $type; 

     return $this; 
    } 

    /** 
    * Get type 
    * 
    * @return string 
    */ 
    public function getType() 
    { 
     return $this->type; 
    } 

    /** 
    * Set clientInn 
    * 
    * @param string $clientInn 
    * @return SnabUsers 
    */ 
    public function setClientInn($clientInn) 
    { 
     $this->clientInn = $clientInn; 

     return $this; 
    } 

    /** 
    * Get clientInn 
    * 
    * @return string 
    */ 
    public function getClientInn() 
    { 
     return $this->clientInn; 
    } 

    /** 
    * Set clientOgrn 
    * 
    * @param string $clientOgrn 
    * @return SnabUsers 
    */ 
    public function setClientOgrn($clientOgrn) 
    { 
     $this->clientOgrn = $clientOgrn; 

     return $this; 
    } 

    /** 
    * Get clientOgrn 
    * 
    * @return string 
    */ 
    public function getClientOgrn() 
    { 
     return $this->clientOgrn; 
    } 

    /** 
    * Set clientRs 
    * 
    * @param string $clientRs 
    * @return SnabUsers 
    */ 
    public function setClientRs($clientRs) 
    { 
     $this->clientRs = $clientRs; 

     return $this; 
    } 

    /** 
    * Get clientRs 
    * 
    * @return string 
    */ 
    public function getClientRs() 
    { 
     return $this->clientRs; 
    } 

    /** 
    * Set clientKs 
    * 
    * @param string $clientKs 
    * @return SnabUsers 
    */ 
    public function setClientKs($clientKs) 
    { 
     $this->clientKs = $clientKs; 

     return $this; 
    } 

    /** 
    * Get clientKs 
    * 
    * @return string 
    */ 
    public function getClientKs() 
    { 
     return $this->clientKs; 
    } 

    /** 
    * Set clientBik 
    * 
    * @param string $clientBik 
    * @return SnabUsers 
    */ 
    public function setClientBik($clientBik) 
    { 
     $this->clientBik = $clientBik; 

     return $this; 
    } 

    /** 
    * Get clientBik 
    * 
    * @return string 
    */ 
    public function getClientBik() 
    { 
     return $this->clientBik; 
    } 

    /** 
    * Set clientUaddress 
    * 
    * @param string $clientUaddress 
    * @return SnabUsers 
    */ 
    public function setClientUaddress($clientUaddress) 
    { 
     $this->clientUaddress = $clientUaddress; 

     return $this; 
    } 

    /** 
    * Get clientUaddress 
    * 
    * @return string 
    */ 
    public function getClientUaddress() 
    { 
     return $this->clientUaddress; 
    } 

    /** 
    * Set clientFaddress 
    * 
    * @param string $clientFaddress 
    * @return SnabUsers 
    */ 
    public function setClientFaddress($clientFaddress) 
    { 
     $this->clientFaddress = $clientFaddress; 

     return $this; 
    } 

    /** 
    * Get clientFaddress 
    * 
    * @return string 
    */ 
    public function getClientFaddress() 
    { 
     return $this->clientFaddress; 
    } 

    /** 
    * Set clientDaddress 
    * 
    * @param string $clientDaddress 
    * @return SnabUsers 
    */ 
    public function setClientDaddress($clientDaddress) 
    { 
     $this->clientDaddress = $clientDaddress; 

     return $this; 
    } 

    /** 
    * Get clientDaddress 
    * 
    * @return string 
    */ 
    public function getClientDaddress() 
    { 
     return $this->clientDaddress; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return SnabUsers 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set notes 
    * 
    * @param string $notes 
    * @return SnabUsers 
    */ 
    public function setNotes($notes) 
    { 
     $this->notes = $notes; 

     return $this; 
    } 

    /** 
    * Get notes 
    * 
    * @return string 
    */ 
    public function getNotes() 
    { 
     return $this->notes; 
    } 

    /** 
    * Set addedDate 
    * 
    * @param \DateTime $addedDate 
    * @return SnabUsers 
    */ 
    public function setAddedDate($addedDate) 
    { 
     $this->addedDate = $addedDate; 

     return $this; 
    } 

    /** 
    * Get addedDate 
    * 
    * @return \DateTime 
    */ 
    public function getAddedDate() 
    { 
     return $this->addedDate; 
    } 
} 

avete Tutte le idee perché? eAccelerator non è impostato. Doctrine v 2.2, PHP v 5.3.22, motore zend 2.3.0

+1

Non penso che sia necessario, ma ho sempre messo la notazione '@ORM \ Entity' prima, ci hai provato? Avete altre entità funzionanti che caricano ok? Anche non correlato ... Questa classe rappresenta una singola entità "Utente", quindi è buona norma usare il singolare per il nome della classe (es. Utente). – prodigitalson

+0

questo non ha aiutato :(. Ho più entità, ma ho lo stesso problema con tutti loro ... questo è probabilmente un semplice problema, ma non riesco a trovare cosa sia esattamente – KennyPowers

+0

Non ho usato D2 molto al di fuori di Symfony2 ... 'Setup :: createAnnotationMetadataConfiguration' registra le annotazioni? – prodigitalson

risposta

61

Si sta utilizzando un Doctrine\Common\Annotations\SimpleAnnotationReader anziché uno Doctrine\Common\Annotations\AnnotationReader.

Le SimpleAnnotationReader opere con spazi dei nomi predefiniti e legge le annotazioni in formato @Entity, mentre il AnnotationReader possibile utilizzare le classi e spazi dei nomi (via use statement) e annotazioni, come @ORM\Entity importati.

Ulteriori informazioni al riguardo sono disponibili su documentation.

Ecco una versione fissa del test.php

<?php 

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; 
use Doctrine\Common\Annotations\AnnotationReader; 
use Doctrine\Common\Annotations\AnnotationRegistry; 

require_once __DIR__ . '/vendor/autoload.php'; 
require_once __DIR__ . '/entities/Users.php'; 

$paths   = array(__DIR__ . '/entities'); 
$isDevMode  = false; 
$connectionParams = array(
    'driver' => 'pdo_mysql', 
    'user'  => 'root', 
    'password' => 'pass', 
    'dbname' => 'dbname', 
); 

$config = Setup::createConfiguration($isDevMode); 
$driver = new AnnotationDriver(new AnnotationReader(), $paths); 

// registering noop annotation autoloader - allow all annotations by default 
AnnotationRegistry::registerLoader('class_exists'); 
$config->setMetadataDriverImpl($driver); 

$em = EntityManager::create($connectionParams, $config); 

$user = $em->find('Users', 5); 
+1

funziona! molte grazie! – KennyPowers

+9

Setup di Doctrine 2.3: createAnnotationMetadataConfiguration ha un quinto parametro chiamato $ useSimpleAnnotationReader che puoi passare false per farlo funzionare! – Malte

+2

Risparmio di vita. Come qualcuno nuovo a Doctrine, questo è stato molto utile. –

5

È possibile utilizzare la configurazione, basta fornire false da usare semplice lettore di annotazione, come quella.

<?php 

//same code 

$isSimpleMode = FALSE; 
$proxyDir = null; 
$cache = null; 
$config = Setup::createAnnotationMetadataConfiguration(
$paths, $isDevMode, $proxyDir, $cache, $isSimpleMode 
); 

//same 
?> 
17

Nel mio caso, ho solo dimenticato di aggiungere @ORM\Entity, in questo modo:

/** 
* Project\BackendBundle\Entity\Pedido 
* 
* @ORM\Table 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
*/ 
class Pedido 
5

Nel mio caso, ho ricevuto questo errore perché ho aperto il blocco doc con /*** invece di /**:

/*** 
* @entity 
* @table(name="bans") 
*/ 
+1

E avevo "/ *" invece di "/ **". Grazie! – deltab

1

in seguito alla risposta di Ocramius (che mi ha salvato così)

Se hai qualche namespacing personalizzati sulle proprie entità assicurarsi che le annotazioni vengono dopo il decleration spazio dei nomi, ecco la mia entità che ha funzionato:

<?php 


namespace App\Models; 

/** 
    * Books 
    * 
    * @ORM\Table(name="books") 
    * @ORM\Entity 
    */ 

    use Doctrine\ORM\Mapping as ORM; 


    class Books 
    { 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=20, nullable=true) 
    */ 
    private $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="isbn", type="string", length=20, nullable=true) 
    */ 
    private $isbn; 

Poi, nel mio controller:

$entityManager->find('App\Models\Books', 1) 

Successo !

Problemi correlati