Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register the DBAL Doctrine connection as a service without the DoctrineBundle in Symfony 4


I'm trying to register only the Doctrine DBAL Connection component as a service in Symfony4.
I don't need the full DoctrineBundle symfony offers, but only the part which provides a basic database abstraction level.
Now I'm stuck on figuring out how to implement the raw library downloaded by composer as a service.
This is how the Connection class should be created, as from the official documentation:

<?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);

If it is possible, how do I configure this type of service in the service.yml configuration?
If it is not, how do I proceed then?

like image 590
P. Danielski Avatar asked Dec 03 '17 16:12

P. Danielski


People also ask

What is DBAL doctrine?

The Doctrine Database Abstraction Layer (DBAL) is an abstraction layer that sits on top of PDO and offers an intuitive and flexible API for communicating with the most popular relational databases.

What is Doctrine in Symfony?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.

How do I know my doctrine version?

Check out the file vendor/doctrine/orm/lib/Doctrine/ORM/Version. php , there is a constant in there that shows the version. It's also accessible from a running app but this is easier.


2 Answers

You might be better off just using the doctrine bundle and removing the orm section from the config file. Does not really add much overhead and is easier that doing it yourself.

Having said that, here are the details for a minimal dbal setup

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');
    }
}

Enjoy

like image 104
Cerad Avatar answered Oct 19 '22 05:10

Cerad


Just my 2 cents.

composer create symfony/skeleton super-project

cd super-project

composer require doctrine/dbal
.env

DATABASE_URL=mysql://user:password@localhost/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,
         ));
    }
}
like image 45
Carlos Bonavia Avatar answered Oct 19 '22 05:10

Carlos Bonavia