Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine is not loading a class MappingException

I have a clean install of just Doctrine ORM

Please help I get MappingException for a Product class all the time

<?php

namespace src;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product{

    /**
     * @ORM\Column(type="integer", length = 5)
     * @ORM\Id
     * @ORM\GenerateValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=2, name="product_code")
     */
    protected $code;

    /**
     * @ORM\Column(type="string", length=10, name="product_name")
     */
    protected $name;

}

I have a regular bootstrap file

<?php
// bootstrap.php
require_once "vendor/autoload.php";
require_once "src/Product.php";

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

$paths = array(__DIR__."/src");
$isDevMode = true;

$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'myDbName',
);

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

$theProduct = $entityManager->find("Product", 500);

And I have a composer with

{
  "require": {
    "doctrine/orm": "v2.5.10"
  },
  "autoload": {
    "psr-4": {
      "src\\": "src/"
    }
  }
}

Folder Structure is enter image description here

I am running bootstrap.php

Whatever I do I always get Fatal error: Uncaught Doctrine\Common\Persistence\Mapping\MappingException: Class 'Product' does not exist in D:\projects\pp\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php on line 96

like image 984
user3410843 Avatar asked Sep 15 '25 23:09

user3410843


1 Answers

There are two issues here

1)

createAnnotationMetadataConfiguration has a 5th parameter which is boolean $useSimpleAnnotationReader. So if you want to write something like this

use Doctrine\ORM\Mapping as ORM
/**
 * @ORM\Entity
 */

past the 5th parameter false during config

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);

Otherwise if you do not include Mapping, and @ORM in front it will work just fine with

/**
* @Entity
*/

2)

If you want to namespace your Entities you have to add namespace for your entities to your config

$config->addEntityNamespace('', 'src\entity');

First parameter is an alias (you can leave it empty or whatever you put should append in front of [:] in the next statement), now you can call

$theProduct = $entityManager->find(":Product", 500);
like image 112
user3410843 Avatar answered Sep 17 '25 12:09

user3410843