3

我想在我的项目中使用 Doctrine 2。我有一些问题。我阅读了文档,但可能我做错了什么。

我想自动加载实体类。并且文档中的方法不起作用。

我的 bootstrap.php

<?php

require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver;

$paths = array("../Entities");
$isDevMode = false;

$classLoader = new \Doctrine\Common\ClassLoader('Entities','../Entities');
$classLoader->register();

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'xxx',
    'password' => 'xxx',
    'dbname'   => 'xxx',
);

$driver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(new Doctrine\Common\Annotations\AnnotationReader(),array('../Entities'));
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setMetadataDriverImpl($driver);
$em = EntityManager::create($dbParams, $config);

我在实体目录中有我的模型类。我已经使用教义客户端生成了它们。他们看起来不错。开头有 ids、setter、getter 和命名空间。

/Entities/ArticleCat.php

<?php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * ArticleCat
 *
 * @ORM\Table(name="article_cat")
 * @ORM\Entity
 */
class ArticleCat
{

我想使用学说的脚本:

<?php
  require_once 'bootstrap.php';

  $article = $em->find('ArticleCat', 21);
  echo $article->getName();

它不工作。只有当我以这种方式使用它并且我从实体模型中删除命名空间时它才有效。

<?php
  require_once 'bootstrap.php';
  require_once 'Entities/ArticleCat.php'; //this line added (manual load)


  $article = $em->find('ArticleCat', 21);
  echo $article->getName();

使用学说和自动加载实体的正确方法是什么?为什么命名空间有问题?

我的错误:

Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' 
with message 'Class 'ArticleCat' does not exist' in 
/myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96
Stack trace: #0 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(40): 
Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('ArticleCat') 
#1 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
   /AbstractClassMetadataFactory.php(267): Doctrine\Common\Persistence\Mapping
   \RuntimeReflectionService->getParentClasses('ArticleCat') 
#2 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
   /AbstractClassMetadataFactory.php(297): Doctrine\Common\Persistence\Mapping
   \AbstractClassMetadataFactory->getParentClasses('ArticleCat') 
#3 /myproject/vendor/doc in /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence
   /Mapping/MappingException.php on line 96
4

3 回答 3

2

尝试将完全限定的类名与命名空间一起使用。当您遇到有关CG代理类的错误时,请尝试通过运行来生成您的代理类

vendor/bin/doctrine orm:generate-proxies

祝你好运!

于 2013-12-14T20:22:54.167 回答
1

如果您使用的是 composer,您需要确保您的 composer.json 中有以下“自动加载”:

{
        "require": {
           ...
        },
        "autoload": {
            "psr-0": {"": "Entity/"}
        }
}

这解决了我的问题。不过,我没有在我的实体类中使用任何命名空间。我的 composer.json 位于我的 inc 目录中,此处引用的 Entity 目录位于 inc/Entity 中。

于 2014-05-24T23:46:06.360 回答
0

ClassLoader嗯,在使用命名空间时,我一直使用。我遇到了很多麻烦,以至于我正在使用一个排序配置类来满足path/to/src我的所有需要​​。

例如,如果您的路径是path/to/src并且您的类名是Entities\ArticleCat,那么文件位置的目录结构ArticleCat.php

src \
  Entities \
    ArticleCat.php

现在您需要做的就是创建一个ClassLoaderforEntities

$src = 'path/to/src'
$cl = new ClassLoader('Entities', $src);
$cl->register();

现在ArticleCat应该可以使用EntityManager

$articleCat = $em->getRepository('Entities\ArticleCat');
$article = $articleCat->find(21);
echo $article->getName();

或者

$article = $em->find('Entities\ArticleCat', 21);
echo $article->getName();
于 2014-10-14T21:39:53.503 回答