Silex和Doctrine ORM

时间:2013-04-09 17:55:01

标签: doctrine-orm silex

我正在尝试将Silex与Doctrine ORM(不仅仅是DBAL)一起使用,但我无法正确配置。

composer.json

{
  "require": {
    "silex/silex": "1.0.*@dev",
    "symfony/monolog-bridge": "~2.1",
    "symfony/twig-bridge": "~2.1",
    "symfony/form": "~2.1",
    "symfony/yaml": "2.2.*",
    "symfony/form": "2.2.*",
    "symfony/translation": "~2.1",
    "symfony/config": "2.2.*",
    "dflydev/doctrine-orm-service-provider": "1.0.*@dev"
  },
  "autoload": {
    "psr-0": {
      "Entities": "src/"
    }  
  }
}

bootstrap.php位于我的项目根文件夹

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

require_once __DIR__ ."/vendor/autoload.php";

$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Entities"), $isDevMode);

$params = array(
    'driver' => 'pdo_sqlite',
    'path' => __DIR__ . '/development.sqlite',
);

$entityManager = EntityManager::create($params, $config);

cli-config.php也位于根文件夹

require_once "bootstrap.php";

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($entityManager->getConnection()),
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager)
));

位于src / Entities

内的Customer.php实体
/**
 * @Entity @Table(name="customers")
 **/
class Customer {
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $name;

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getId() {
        return $this->id;
    }
}

我能够运行像php vendor/bin/doctrine orm:schema-tool:create这样的命令,让它生成一个名为习惯的表,就像它应该的那样。但是如何在我的Silex应用程序中加载该实体

这是我的index.php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

use Symfony\Component\Yaml\Yaml;
$app['config'] = function () {
    $config = Yaml::parse(__DIR__ .'/../config.yml');

    return $config;
};

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'dns.options' => $app['config']['database']['development']
));
$app->register(new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider, array(
    'orm.em.options' => array(
        'mappings' => array(
            array(
                'type' => 'annotation',
                'path' => __DIR__ .'/src/Entities',
            )
        )
    ),
));
$app->get('/', function () use ($app) {
    $customer = $app['orm.em']->getRepository('Customer');

return '<pre>'. $customer->getName() .'</pre>';
});

在我的浏览器中加载localhost时的结果

Warning: class_parents() [function.class-parents]: Class Customer does not exist and could not be loaded in /Users/me/Documents/project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php on line 40

更新 我不确定这是解决这个问题的正确方法,但是通过使用以下方法,问题得到解决,我现在可以在Silex中使用我的实体

$app['em'] = function ($app) {
    $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Entities"), true);
    $params = array(
        'driver' => 'pdo_sqlite',
      'path' => __DIR__ . '/../development.sqlite',
    );
    $entityManager = EntityManager::create($params, $config);

    return $entityManager;
};

我使用了依赖方法,因为我可以使用$ app ['config']来存储数据库信息和其他特定于环境的配置。

$customer = new \Entities\Customer();
$customer->setName('Multi Corp '. uniqid());

$app['em']->persist($customer);
$app['em']->flush();

1 个答案:

答案 0 :(得分:3)

我假设您的学说实体映射位于命名空间\Entities中的“/ src / Entities”下。使用自动加载器指令,它们应该可以\Entities\MyMappingCls访问。

你的问题似乎是你在获取存储库时没有给出映射类的fq-name。您需要提供一个可由自动装带器解析的字符串。请尝试:

$app['orm.em']->getRepository('Entities\Customer');

您也可以尝试运行orm:generate-proxies,因为它们仅在调试模式下即时生成(不确定这是否相关)。

HTH