Doctrine 2实体错误

时间:2013-12-02 21:24:49

标签: php symfony doctrine-orm doctrine

我想问一下,如果有人之前得到过这样的错误。因为我已经坚持了2个小时修复了学说已经存在的错误。所以任何形式的帮助都会受到赞赏。 到了这一点。我正在与学说实体经理战斗我无法使它工作。 我创建了实体和教义类,但我一直都会收到错误:

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

2 个答案:

答案 0 :(得分:1)

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

但是我按照Symfony 2文档中的示例进行了操作?是的。对于Symfony 2,您需要使用@ORM。但是你的测试用例使用的是“纯粹”的学说,这意味着没有@ORM。

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

为什么S2使用@ORM?这是一个漫长的悲惨故事,但基本上它引入了@ORM前缀,因此其他注释不会与Doctrine冲突。

您是否可以调整Doctrine配置以允许@ORM的用户?是的。但我忘记了。你可以搜索它。

底线:考虑使用Symfony 2 Doctrine服务。这更容易。

答案 1 :(得分:0)

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