0

I would like to ask, if anybody obtained such error before. Since I am stuck for 2 hours fixing bugs with doctrine already. So any kind of of help would be appreciate. To the point. I am fighting with doctrine Entity Manager I can't make it work. I created entity and doctrine class to work with, but I am getting an error all the time:

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message  'Class "MSP\Model\Entity\Category" is not a valid entity or mapped super class.' in /home/dariss/www/dom/php/MenuSiteProject/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:216 Stack trace: #0 /home/dariss/www/dom/php/MenuSiteProject/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php(87): Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass('MSP\Model\Entit...') #1 /home/dariss/www/dom/php/MenuSiteProject/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(113): Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass('MSP\Model\Entit...', Object(Doctrine\ORM\Mapping\ClassMetadata)) #2 /home/dariss/www/dom/php/MenuSiteProject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(318): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array) in /home/dariss/www/dom/php/MenuSiteProject/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 216 

My entity class.

namespace MSP\Model\Entity; 
use Doctrine\ORM\Mapping as ORM;

/**
* Category
*
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category {

/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="int", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 * @ORM\SequenceGenerator(sequenceName="category_id_seq", allocationSize=1, initialValue=1)
 */
private $id;
/**
 * @var String $name
 *
 * @ORM\Column(name="name", type"string", length=50, nullable=true)
 */
private $name;
/**
 * @var integer $parent
 *
 * @ORM\Column(name="parent", type="int", nullable=true)
 */
private $parent;

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

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

    return $this;
}

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

/**
 * @param int $parent
 * @return Category
 */
public function setParent($parent)
{
    $this->parent = $parent;

    return $this;
}

/**
 * @return int
 */
public function getParent()
{
    return $this->parent;
}

Doctrine class:

namespace MSP\Helper;


use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;

class Doctrine{

public $em = null;

public function __construct()
{

    require_once __DIR__.'/../../../vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php';

    $doctrineClassLoader = new ClassLoader('Doctrine',  '/');
    $doctrineClassLoader->register();
    $entitiesClassLoader = new ClassLoader('MSP\Model\Entity', '/../Model/Entity');
    $entitiesClassLoader->register();
    $proxiesClassLoader = new ClassLoader('Proxies', '/../Proxies/');
    $proxiesClassLoader->register();

    // Set up caches
    $config = new Configuration;
    $cache = new ArrayCache;
    $config->setMetadataCacheImpl($cache);
    $driverImpl = $config->newDefaultAnnotationDriver(array('/../Model/Entity'), true);
    $config->setMetadataDriverImpl($driverImpl);
    $config->setQueryCacheImpl($cache);

    $config->setQueryCacheImpl($cache);

    // Proxy configuration
    $config->setProxyDir('/proxies');
    $config->setProxyNamespace('Proxies');

    // Set up logger
    $logger = new EchoSQLLogger;
    //$config->setSQLLogger($logger);

    $config->setAutoGenerateProxyClasses( TRUE );

    $iniParser = new \MSP\Helper\IniParser();
    $configuration = $iniParser->getConfig();
    // Database connection information
    $connectionOptions = array(
        'driver'   => $configuration['driver'],
        'user'     => $configuration['username'],
        'password' => $configuration['password'],
        'dbname'   => $configuration['dbname'],
        'host'     => $configuration['host']
    );

    // Create EntityManager
    $this->em = EntityManager::create($connectionOptions, $config);
    }
 }

Test file:

   $doctrine = new MSP\Helper\Doctrine();
   $doctrine->em->find('MSP\Model\Entity\Category', 1);
Dariss
  • 1,258
  • 1
  • 12
  • 27

2 Answers2

1

You need to drop the @ORM prefixes when you annotate the entities.

But I followed the example in the Symfony 2 documentation? Yep. For Symfony 2 you need to use @ORM. But your test cases uses "pure" doctrine which means no @ORM.

If you really need to run your stuff using pure doctrine then consider using yaml notation.

Why does S2 use @ORM? It's a long sad story but basically it introduced the @ORM prefix so other annotations would not clash with Doctrine.

Can you tweak the Doctrine configuration to allow the user of @ORM? Yep. But I forget how. You can search for it.

Bottom line: Consider just using the Symfony 2 Doctrine service. It's easier.

Cerad
  • 48,157
  • 8
  • 90
  • 92
0

You set an absolute path /../Model/Entity. You need to set ./../Model/Entity

Vasil Kulakov
  • 190
  • 2
  • 4