如何在没有Symfony 4中的DoctrineBundle的情况下将DBAL Doctrine连接注册为服务

问题描述 投票:3回答:2

我正在尝试仅将Doctrine DBAL Connection组件注册为Symfony4中的服务。 我不需要完整的DoctrineBundle symfony产品,而只需要提供基本数据库抽象级别的部分。 现在我一直在努力弄清楚如何实现作曲家作为服务下载的原始库。 这是应该如何创建Connection类,如官方文档:

<?php
$config = new \Doctrine\DBAL\Configuration();
//..
$connectionParams = array(
    'dbname' => 'mydb',
    'user' => 'user',
    'password' => 'secret',
    'host' => 'localhost',
    'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);

如果可能,如何在service.yml配置中配置此类服务? 如果不是,我该怎么办?

php symfony doctrine
2个回答
4
投票

您可能最好只使用doctrine包并从配置文件中删除orm部分。实际上并没有增加太多开销,而且更容易自己做。

话虽如此,这里是最小dbal设置的细节

composer create symfony/skeleton s40dbal
cd s40dbal
composer require server
composer require doctrine/dbal

# .env
DB_URL=mysql://user:password@localhost/dbname

# config/services.yaml
Doctrine\DBAL\Configuration:

Doctrine\DBAL\Connection:
    factory: 'Doctrine\DBAL\DriverManager::getConnection'
    arguments:
        -
            url : '%env(DB_URL)%'
            driverOptions: {20: false} # emulate prepared statements
        - '@Doctrine\DBAL\Configuration'

# DefaultController.php
use Doctrine\DBAL\Connection;
class DefaultController
{
    public function index(Connection $conn)
    {
        $stmt = $conn->prepare('SELECT id,name FROM users WHERE username = ?');
        $stmt->execute(['someuser']);
        $row = $stmt->fetch();
        var_dump($row);

        return new Response('dbal');
    }
}

请享用


0
投票

只需2美分。

composer create symfony/skeleton super-project

cd super-project

composer require doctrine/dbal
.env

DATABASE_URL = MySQL的://用户:密码@本地/ DBNAME

config/services.yaml
    App\Service\Conexion:
    arguments:
            -
                url : '%env(DATABASE_URL)%'
in App\Service folder -> Conexion.php
Use Doctrine\DBAL\Configuration;
class Conexion
{

    var $url = '';

    public function __construct($url)
    {
        $this->url = $url['url'];
    }

    public function getConexion(){

        // configuration parameters
        $config =  new Configuration();

        $connectionParams = array(
             'url' => $this->url,
         );
        $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
         return $conn;
    }
in App\Entity folder -> BaseService.php
class BaseService
{
    protected $db;
    public function __construct($db)
    {
        $this->db = $db->getConexion();
    }
}
socios Entity in App\Entity -> Socios.php
namespace App\Entity;

class Socios extends BaseService
{
    public function veo(){
        return $this->db->fetchAll("select * from socios order by id;");
    }
}
Finally in App\Controller -> SociosController.php
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\Conexion;
use App\Entity\Socios;

class SociosController extends Controller
{
    public function Socios(Conexion $conn)
    {
        $socios = (new Socios($conn))->getAll();
        return $this->render('Socios/index.html.twig', array(
        'socios' => $socios,
         ));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.