gpt4 book ai didi

doctrine-orm - Silex和主义ORM

转载 作者:行者123 更新时间:2023-12-04 04:31:01 24 4
gpt4 key购买 nike

我正在尝试将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这样的命令,并使其按需生成名为custom的表。但是如何在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

更新
我不确定这是否是解决此问题的正确方法,但是通过使用以下方法,问题得以解决,现在我可以在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();

最佳答案

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

您的问题似乎是在获取存储库时没有提供映射类的fq名称。您需要提供一个可以由自动加载器解析的字符串。请试试:

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

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

hth

关于doctrine-orm - Silex和主义ORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15909096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com