用于Doctrine DBAL的Symfony配置组件和服务 [英] Symfony config component and service for Doctrine DBAL
本文介绍了用于Doctrine DBAL的Symfony配置组件和服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是 Symfony配置组件和Doctrine dbal 的延续 p>
我正在尝试为这个原则创建服务:
;?php
命名空间Localhost\Service;
使用Doctrine\Common\ClassLoader;
使用Doctrine\DBAL\Configuration;
使用Doctrine\DBAL\DriverManager;
class Doctrine
{
public function __construct()
{
$ doctrineLoader = new ClassLoader('Doctrine');
$ doctrineLoader-> register();
$ doctrineConfig = new Configuration();
$ doctrineParams = [
'driver'=> 'pdo_mysql',
'dbname'=> 'levelup',
'host'=> '127.0.0.1',
'user'=> 'root',
'password'=> 'toor',
];
return DriverManager :: getConnection($ doctrineParams,$ doctrineConfig);
}
}
然后我试图把它称为
$ doctrineConnection = $ sysContainer-> get('doctrine');
$ sqlQuery ='SELECT * FROM`thoughts`';
$ queryResult = $ doctrineConnection-> query($ sqlQuery) - > fetch();
但是我收到错误
致命错误:调用未定义的方法Localhost\Service\Doctrine :: query()
为什么没有服务它工作正常?
p.s.如果您有更好的想法或建议,如何重写此代码以适应symfony服务结构,我将很乐意听到他们。
解决方案
class Doctrine
{
//只需重命名__construct创建并使其成为静态
static function create()
{
$ doctrineLoader = new ClassLoader('Doctrine');
$ doctrineLoader-> register();
$ doctrineConfig = new Configuration();
$ doctrineParams = [
'driver'=> 'pdo_mysql',
'dbname'=> 'levelup',
'host'=> '127.0.0.1',
'user'=> 'root',
'password'=> 'toor',
];
return DriverManager :: getConnection($ doctrineParams,$ doctrineConfig);
}
}
然后在services.yml文件中:
doctrine:
class:Doctrine\DBAL\Connection
factory_class:'Localhost\Service\Doctrine'
factory_method:'create'
This is a continuation of Symfony config component and Doctrine dbal
I am trying to create service for doctrine now like this:
<?php
namespace Localhost\Service;
use Doctrine\Common\ClassLoader;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
class Doctrine
{
public function __construct()
{
$doctrineLoader = new ClassLoader('Doctrine');
$doctrineLoader->register();
$doctrineConfig = new Configuration();
$doctrineParams = [
'driver' => 'pdo_mysql',
'dbname' => 'levelup',
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'toor',
];
return DriverManager::getConnection($doctrineParams, $doctrineConfig);
}
}
And then i am trying to call it like
$doctrineConnection = $sysContainer->get('doctrine');
$sqlQuery = 'SELECT * FROM `thoughts`';
$queryResult = $doctrineConnection->query($sqlQuery)->fetch();
But i am getting error
Fatal error: Call to undefined method Localhost\Service\Doctrine::query()
Why, without service it's working perfectly? p.s. If you have better ideas or advices how to rewrite this code to fit symfony service structure, i would be happy to hear them.
解决方案
class Doctrine
{
// Just rename __construct to create and make it static
static function create()
{
$doctrineLoader = new ClassLoader('Doctrine');
$doctrineLoader->register();
$doctrineConfig = new Configuration();
$doctrineParams = [
'driver' => 'pdo_mysql',
'dbname' => 'levelup',
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'toor',
];
return DriverManager::getConnection($doctrineParams, $doctrineConfig);
}
}
Then in services.yml file:
doctrine:
class: Doctrine\DBAL\Connection
factory_class: 'Localhost\Service\Doctrine'
factory_method: 'create'
这篇关于用于Doctrine DBAL的Symfony配置组件和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文