如何在Symfony2服务类中访问Doctrine DBAL? [英] How do you access Doctrine DBAL in a Symfony2 service class?

查看:224
本文介绍了如何在Symfony2服务类中访问Doctrine DBAL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Symfony2(和OOP),并希望创建一个在我的应用程序中可用的服务。这个服务需要一个值foo,对数据库表进行检查,并返回值栏。



我有一个小课程

 命名空间Acme\TestBundle\Toolbox; 

class StringToolbox
{
public function lookupSomething($ foo)
{

$ conn = $ this-> get(' database_connection');
$ sql =SELECT bar FROM bar_list WHERE foo =:foo;
$ stmt = $ conn-> prepare($ sql);
$ stmt-> bindValue(foo,$ foo);
$ stmt-> execute();


return $ bar;
}


}

我的设置是:

 服务:
工具箱:
类:Acme\TestBundle\Toolbox
参数:[@database_connection]

但是它会抛出一个错误,表示get()方法未定义。我被卡住了 - 如何在服务中使用DBAL?谢谢!

解决方案

首先,你应该添加一个构造函数到你的课堂,并传入 @ doctrine.dbal。 %connection_name%_connection service

 命名空间Acme\TestBundle\Toolbox; 
使用Doctrine\DBAL\Connection;

class StringToolbox
{
/ **
*
* @var连接
* /
private $ connection;

public function __construct(Connection $ dbalConnection){
$ this-> connection = $ dbalConnection;
}

public function lookupSomething($ foo)
{

$ sql =SELECT bar FROM bar_list WHERE foo =:foo;
$ stmt = $ this-> connection-> prepare($ sql);
$ stmt-> bindValue(foo,$ foo);
$ stmt-> execute();


return $ bar;
}


}

您的服务配置现在应该是这样的:

 参数:
my_service_connection:默认

服务:
工具箱:
类:Acme\TestBundle\Toolbox\StringToolbox
参数:[@ doctrine.dbal。%my_service_connection%_connection]
/ pre>

您使用此配置所说的内容是让我成为一个名为toolbox的服务,将接收doctrine.dbal.default_connection服务作为第一个构造函数



除了构造函数注入之外,还有其他注入方法,您应该阅读 http://symfony.com/doc/current/book/service_container.html 文档以了解所有可能性(Setter注入,工厂注入等),并更好地了解依赖注入如何工作


I'm learning Symfony2 (and OOP) and want to create a service that's available throughout my app. This service takes a value foo, checks it against a database table, and returns a value bar.

I have a little class

namespace Acme\TestBundle\Toolbox;

class StringToolbox
{
    public function lookupSomething($foo)
   {

        $conn = $this->get('database_connection');
        $sql = "SELECT bar FROM bar_list WHERE foo = :foo";
        $stmt = $conn->prepare($sql);
        $stmt->bindValue("foo", $foo);
        $stmt->execute();


        return $bar;
    }


}

My settings are:

services:
    toolbox:
       class:        Acme\TestBundle\Toolbox
        arguments:   [@database_connection]

But it throws an error saying that the get() method is undefined. I'm stuck-- how can I use DBAL in the service? Thanks!

解决方案

First off you should add a constructor to your class and pass in the @doctrine.dbal.%connection_name%_connection service

namespace Acme\TestBundle\Toolbox;
use Doctrine\DBAL\Connection;

class StringToolbox
{
    /**
    *
    * @var Connection
    */
    private $connection;

    public function __construct(Connection $dbalConnection)  {
        $this->connection = $dbalConnection;    
    }

    public function lookupSomething($foo)
    {

    $sql = "SELECT bar FROM bar_list WHERE foo = :foo";
    $stmt = $this->connection->prepare($sql);
    $stmt->bindValue("foo", $foo);
    $stmt->execute();


    return $bar;
    }


}

Your service configuration should now look like this:

parameters:
 my_service_connection: default

services:
 toolbox:
   class:        Acme\TestBundle\Toolbox\StringToolbox
    arguments:   [@doctrine.dbal.%my_service_connection%_connection]

What you are saying with this configuration is "make me a service named toolbox that will receive the doctrine.dbal.default_connection service as the first constructor argument"

There are other injection methods besides Constructor injection and you should read the http://symfony.com/doc/current/book/service_container.html documentation to get a grasp of all possibilities (Setter injection, Factory injection, etc) and to better understand how Dependency Injection works

这篇关于如何在Symfony2服务类中访问Doctrine DBAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆