2013-04-09 81 views
9

我想使用Silex與Doctrine ORM(不只是DBAL),但我無法獲得配置正確。位於我的項目的根文件夾Silex和Doctrine ORM

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

客戶內。 php實體位於src /實體

/** 
* @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>'; 
}); 

加載瀏覽器內的本地主機

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 

UPDATE 我後的結果不知道這是解決此問題的正確方法,但通過使用以下方法,問題得到解決,我現在可以在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(); 

回答

3

我相信你的學說實體映射在命名空間\Entities駐留「/ src目錄/實體」下。使用自動加載器指令時,應該可以使用\Entities\MyMappingCls

您的問題似乎是,您在獲取存儲庫時不會提供映射類的fq-name。您需要提供一個可以由自動加載器解決的字符串。請嘗試:

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

您也可以嘗試運行orm:generate-proxies因爲它們只在調試模式實時生成的(不是很確定這是相關的)。

hth