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