orm: Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException
Here is generated entity code:
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="database_a.user", indexes={@ORM\Index(name="address_id", columns={"address_id"})})
* @ORM\Entity
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="text", length=65535, nullable=false)
*/
private $username;
/**
* @var \Address
*
* @ORM\ManyToOne(targetEntity="Address")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
* })
*/
private $address;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set address
*
* @param \Address $address
*
* @return User
*/
public function setAddress(\Address $address = null)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return \Address
*/
public function getAddress()
{
return $this->address;
}
}
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Address
*
* @ORM\Table(name="database_b.address")
* @ORM\Entity
*/
class Address
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="phone", type="text", length=65535, nullable=false)
*/
private $phone;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set phone
*
* @param string $phone
*
* @return Address
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
}
Here is my code I just made little change in generated entity and added database name before table name :
<?php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array(__DIR__."/scr");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'xxxxx',
'password' => 'xxxxx',
'dbname' => 'database_a',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = $entityManager = EntityManager::create($dbParams, $config);
require_once "Entities/User.php";
require_once "Entities/Address.php";
$data = $em->getRepository('User');
$datax = $data->findAll();
foreach($datax as $d) {
echo '<pre>'; print_r($d);
}
error detail:
Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException: Class "User" is not a valid entity or mapped super class. in D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php:346 Stack trace: #0 D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\Driver\AnnotationDriver.php(91): Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass('User') #1 D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php(151): Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass('User', Object(Doctrine\ORM\Mapping\ClassMetadata)) #2 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(332): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array) #3 D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php(78): Doctrine\Common\Persistence\Mapping\AbstractClassMetada in D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php on line 346
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (6 by maintainers)
Please don’t paste messages with
#1
,#2
and such in it, as they cause noise on early project issue 😃Anyway, the autoloading is up to you: we only provide the metadata loading once a class can be loaded