kri*_*sen 9 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/"
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
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)
));
Run Code Online (Sandbox Code Playgroud)
Customer.php实体位于src/Entities内
/**
* @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;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以运行命令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>';
});
Run Code Online (Sandbox Code Playgroud)
在我的浏览器中加载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
Run Code Online (Sandbox Code Playgroud)
更新 我不确定这是解决此问题的正确方法,但通过使用以下方法,问题得到解决,我现在可以在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;
};
Run Code Online (Sandbox Code Playgroud)
我使用了依赖方法,因为我可以使用$ app ['config']来存储数据库信息和其他特定于环境的配置.
$customer = new \Entities\Customer();
$customer->setName('Multi Corp '. uniqid());
$app['em']->persist($customer);
$app['em']->flush();
Run Code Online (Sandbox Code Playgroud)
小智 3
我认为您的学说实体映射位于名称空间中的“/src/Entities”下\Entities。通过您的自动加载器指令,它们应该可以作为\Entities\MyMappingCls.
您的问题似乎是在获取存储库时没有给出映射类的 fq-name 。您需要提供一个可以由自动加载器解析的字符串。请尝试:
$app['orm.em']->getRepository('Entities\Customer');
Run Code Online (Sandbox Code Playgroud)
您还可以尝试运行,orm:generate-proxies因为它们仅在调试模式下动态生成(不太确定这是否相关)。
哈
| 归档时间: |
|
| 查看次数: |
8462 次 |
| 最近记录: |