3
votes

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

1
Is your entity actually located in __DIR__."/src" ? Did you print that path and check if everything's correct? Is the autoloader configured correctly?ccKep
Yes the problem is that the path is correct, I have checked in several ways. I don't know what else to check.user3410843
Try adding "autoload": {"psr-4": {"src\\": "src/"}} to your composer.json (using src as a namespace is quite unusual though)ccKep
autoload inside composer json did not help. I had folder entity inside src which was namespaced src\entity but I removed everything to get clear understanding of what is going on, if by unusual you mean folder structure.user3410843
You might want to edit your question by appending your whole composer.json (or relevant parts) and folder structure.ccKep

1 Answers

3
votes

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);