Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException #7109

Closed
flik opened this issue Mar 3, 2018 · 17 comments
Closed

Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException #7109

flik opened this issue Mar 3, 2018 · 17 comments
Assignees

Comments

@flik
Copy link

flik commented Mar 3, 2018

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
@flik
Copy link
Author

flik commented Mar 3, 2018

without change in entity the error is same...

@Ocramius
Copy link
Member

Ocramius commented Mar 3, 2018

$paths = array(__DIR__."/scr"); ;-)

@flik
Copy link
Author

flik commented Mar 3, 2018

I have changed the path:

$paths = array(__DIR__."/Entities");

but still error is coming :
Fatal error: Uncaught Doctrine\Common\Persistence\Mapping\MappingException: Class 'User' does not exist in D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:96 Stack trace: #0 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService.php(41): Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('User') #1 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(281): Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getParentClasses('User') #2 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(311): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getParentClasses('User') #3 D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php(78): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata(' in D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php on line 96

@Ocramius
Copy link
Member

Ocramius commented Mar 3, 2018

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

@flik
Copy link
Author

flik commented Mar 3, 2018

these numbers were coming with errors not pasted by me.

@flik
Copy link
Author

flik commented Mar 3, 2018

i am unable to solve the problem

@Ocramius
Copy link
Member

Ocramius commented Mar 3, 2018

@flik first step (unrelated to ORM) is to make this work:

var_dump(new User());

After that works, retry the snippet above.

@flik
Copy link
Author

flik commented Mar 3, 2018

here is the output of above code:
object(User)#27 (3) { ["id":"User":private]=> NULL ["username":"User":private]=> NULL ["address":"User":private]=> NULL }

@Ocramius
Copy link
Member

Ocramius commented Mar 3, 2018

Right, now you need to make that same code work in the context of the script that raised:

Fatal error: Uncaught Doctrine\Common\Persistence\Mapping\MappingException: Class 'User' does not exist in

@flik
Copy link
Author

flik commented Mar 3, 2018

When I not include the entity files with

require_once "Entities/User.php";
require_once "Entities/Address.php";

then error comes
Fatal error: Uncaught Doctrine\Common\Persistence\Mapping\MappingException: Class 'User' does not exist in
other wise
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

@flik
Copy link
Author

flik commented Mar 3, 2018

I have shared the full code of User class in first message in this thread. The code was generated by doctrine.

@Ocramius
Copy link
Member

Ocramius commented Mar 3, 2018

I created a gist at https://gist.github.com/Ocramius/04e50ce818ecfc95c8e5649f7a39c111 with a working setup.

What changed is this:

diff --git a/test.php b/test.php
index 84bd2d8..4792364 100644
--- a/test.php
+++ b/test.php
@@ -17,7 +17,7 @@ $dbParams = array(
     'dbname'   => 'database_a',
 );
 
-$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
+$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null,null,false);
 $em = $entityManager = EntityManager::create($dbParams, $config);
 
 $data = $em->getRepository('User');

The problem is that Setup creates a simplified annotation driver, which recognizes @Entity and @Table, but not @ORM\Entity or such.

@flik
Copy link
Author

flik commented Mar 3, 2018

Now, after the above changes. I am getting this output

Warning: require(C:\Users\feb2018\AppData\Local\Temp\__CG__Address.php): failed to open stream: No such file or directory in D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Proxy\AbstractProxyFactory.php on line 209

Fatal error: require(): Failed opening required 'C:\Users\feb2018\AppData\Local\Temp\__CG__Address.php' (include_path='D:\xampp\php\PEAR') in D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Proxy\AbstractProxyFactory.php on line 209

@Ocramius
Copy link
Member

Ocramius commented Mar 3, 2018

Use $isDevMode = true for now. You will need to generate and properly configure proxies later.

@flik
Copy link
Author

flik commented Mar 3, 2018

Bundle of thanks :) man
👍

@flik
Copy link
Author

flik commented Mar 3, 2018

It is working now.

@Rakesh1581991
Copy link

Thanks mate !!! its working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants