原则2实体错误


Doctrine 2 Entity Error

我想问一下,以前是否有人收到过这样的错误。因为我已经被困了2个小时,用教条修复bug了。因此,任何形式的帮助都将不胜感激。切中要害。我正在与条令实体经理斗争,我无法让它发挥作用。我创建了实体和条令类来处理,但我一直在犯错误:

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 

我的实体类

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

条令类:

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

测试文件:

   $doctrine = new MSP'Helper'Doctrine();
   $doctrine->em->find('MSP'Model'Entity'Category', 1);

在注释实体时,需要删除@ORM前缀。

但是我遵循了Symfony 2文档中的示例?是的。对于Symfony 2,您需要使用@ORM。但是您的测试用例使用"纯"原则,这意味着没有@ORM。

如果你真的需要使用纯粹的原则来运行你的东西,那么考虑使用yaml表示法。

S2为什么使用@ORM?这是一个很长的悲伤故事,但基本上它引入了@ORM前缀,这样其他注释就不会与条令相冲突。

你能调整条令配置以允许@ORM的用户吗?是的。但我忘了怎么做。你可以搜索它。

一句话:考虑使用Symfony 2 Doctrine服务。这更容易。

您设置了一个绝对路径/../Model/Entity。您需要设置./../Model/Entity