Doctrine 2 - How to add custom DBAL driver? - php

How can I add my custom driver without modifying DriverManager.php in the Doctrine2 core?
I have created a DBAL Driver for pdo_dblib and placed it inside a Symfony2 bundle. This works fine, however I must add my driver to a list of hard-coded drivers in DriverManager.php, otherwise I get the following exception:
Exception
[Doctrine\DBAL\DBALException]
The given 'driver' pdo_dblib is unknown, Doctrine currently supports only the following drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo_ibm, pdo_sqlsrv
Unless I modify DriverManager.php
final class DriverManager
{
private static $_driverMap = array(
'pdo_dblib' => 'Doctrine\DBAL\Driver\PDODblib\Driver', // Added this line
);
}
Here's my config.yml:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_dblib
driver_class: PDODblibBundle\Doctrine\DBAL\Driver\PDODblib\Driver

You actually can, just leave the driver configuration option completlely out.
All you need to define is the driver_class option. The driver is only used to do an internal lookup for the default driver classes, as long as you provide the class only, it will not fail doing the lookup.
Btw: There is no way (in a complete default setup) to define this in the parameters.ini, you have to change it directly inside the config.yml
Btw: due to another defect (driver falling back to mysql in on specific area), you may not set the charset in the configuration, as it will register an MySql event handler for setting the charset than.
So my final doctrine config based on my mssql_* based implementation looks like the following and works without problems:
# Doctrine Configuration
doctrine:
dbal:
#driver: %database_driver%
driver_class: Doctrine\DBAL\Driver\MsSql\Driver
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
#charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true

You can use the option driverClass:
$connectionParams = array(
'driverClass' => 'YOUR_CUSTOM_CLASS_DB',
);
$entityManager = \Doctrine\ORM\EntityManager::create($connectionParams, $config);

Related

Best practice for database connection via Doctrine in a Symfony Bundle

In my bundle i create a separate database connection and an EntityManager for it. Everything works fine, except those two things don't show up in the development profiler. There is only the default EntityManager and the default connection.
So basically i created 3 new service definitions for an Doctrine\Common\EventManager, an Doctrine\DBAL\Connection and an Doctrine\ORM\EntityManager. I've already tried to add these new service definition to the ContainerBuilder with the same naming convention which is used by the doctrine bridge, but they still won't show up in the profiler. The connection works fine, but i want debug it with and integrate it in the Symfony lifecycle.
The question is:
What is the best practice to create a separate database connection via Doctrine inside of a Symfony Bundle if the Symfony application is only configured to support one connection?
I believe you should take a look at this doc. They described there how to add another EntityManager, which mean another connection. First step is to create configuration.
Especialy take a look at doctrine.yaml configuration:
# config/packages/doctrine.yaml
doctrine:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
# configure these for your database server
url: '%env(DATABASE_CUSTOMER_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
customer:
connection: customer
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customer'
prefix: 'App\Entity\Customer'
alias: Customer
Above are two entity managers, 'default' and 'customer'. There are also two cennections, one for each manager.
If configuration is valid you will have access to those managers by passing its names to 'getManager' method.
$entityManager = $this->getDoctrine()->getManager('default');
$customerEntityManager = $this->getDoctrine()->getManager('customer');
If you cant edit configuration:
What what about creating custom class (Manager or something) in which you will manually create connection. Take a look at this, it should help you.
getting-a-connection

Multiple connections and entity managers in a symfony 2 / 3 application

My multi-tenant-app uses a master database, that holds information about tenants (like name, etc.) and a app-specific database per tenant.
I configured a master and some_tenant connection and entity manager in the doctrine section inside config.yml.
This gives me access to the master database from a controller (eg. for validating and getting tenant information for some_tenant based on the subdomain some_tenant.my-app.com). And it lets me use a tenant-specific database and entity manager during the application life-cycle.
The doctrine section in my config looks like this:
doctrine:
dbal:
default_connection: 'master'
connections:
master:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
some_tenant:
driver: pdo_mysql
host: "%database_host_some_tenant%"
port: "%database_port_some_tenant%"
dbname: "%database_name_some_tenant%"
user: "%database_user_some_tenant%"
password: "%database_password_some_tenant%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
master:
connection: master
mappings:
BEMultiTenancyBundle: ~
some_tenant:
connection: some_tenant
mappings:
AppBundle: ~
Here comes the part, which I am unhappy with and cannot find a solution:
First of all, tenants will be more than 20. And it starts to get messy, altering the doctrine config this way.
Then there is another config file, called tenants.yml which holds more information like enabled/disabled app-features, tenant-specific themes, etc.
The file is loaded, validated using the Config Component, and a container parameter is set, so that tenants configurations are available app-wide.
I would like to store the database credentials also in that file.
I want to create connections and entity managers based on that config. One per each tenant, which can be used during the app life-cycle.
I need to have them available also at the console command bin/console doctrine:schema:update --em=some_tenant for updating each tenant's database schem and bin/console doctrine:schema:update --em=master for updating the master database scheme.
By now I guess, the only way to achieve this is to add configuration parameters to the doctrine section programmatically after the AppBundle is loaded, and before the doctrine registry is constructed with the given managers and connections.
But I cannot even find a point, where I could achive this.
Is there another way to get to point 1 and 2?
Even though there is a similiar question, which I am not sure if it is exactly about the same problem and is about 3 years old, I wanted to post this question with a bit more explanation.
The solution in comments is a straight way easy solution and it will work. The other solution I've ever meet is to dynamically replace tenant database credentails using some external conditions, i.e request HOST.
You can decorate or extend the connection factory with a service in a way, that having the request stack available (or providing the domain with ENV or console argument for other SAPI's) you can have the same entity manager (even default!) being configured on demand.
on a brief look this would look like
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Symfony\Component\HttpFoundation\RequestStack;
class DynamicConnectionFactory extends Factory
{
/** #var RequestStack */
private $requestStack;
public function __construct(array $types, RequestStack $stack)
{
parent::__construct($types);
$this->requestStack = $stack;
}
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
{
$host = $this->requestStack->getMasterRequest()->getHost();
$params = $this->replaceParamsForHost(array $params, $host);
return parent::createConnection($params, $config, $eventManager, $mappingTypes);
}
private function replaceParamsForHost(array $params, $host)
{
//do your magic, i.e parse config or call Memcache service or count the stars
return array_replace($params, ['database' => $host]);
}
}
I think that, To manage multi-tenant with symfony 2/3.
We can config auto_mapping: false for ORM of doctrine.
file: config.yml
doctrine:
dbal:
default_connection: master
connections:
master:
driver: pdo_mysql
host: '%master_database_host%'
port: '%master_database_port%'
dbname: '%master_database_name%'
user: '%master_database_user%'
password: '%master_database_password%'
charset: UTF8
tenant:
driver: pdo_mysql
host: '%tenant_database_host%'
port: '%tenant_database_port%'
dbname: '%tenant_database_name%'
user: '%tenant_database_user%'
password: '%tenant_database_password%'
charset: UTF8
orm:
default_entity_manager: master
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
master:
connection: master
auto_mapping: false
mappings:
AppBundle:
type: yml
dir: Resources/master/config/doctrine
tenant:
connection: tenant
auto_mapping: false
mappings:
AppBundle:
type: yml
dir: Resources/tenant/config/doctrine
After that, we cannot handle connection of each tenant by override connection info in request_listener like article: http://mohdhallal.github.io/blog/2014/09/12/handling-multiple-entity-managers-in-doctrine-the-smart-way/
I hope that, this practice can help someone working with multi-tenant
Regards,
Vuong Nguyen

Connect Doctrine to a MS SQL Database

Just before i get started, I have been trying to figure this out and have clicked nearly every google link there is and I have read nearly all the other questions on this. But I'm stuck because the bundles that are being suggested for this are out of date.
I am creating a website. Tt is a symfony2 application that is being hosted on Microsoft azure. What I want to do is be able to use doctrine of course to fetch and create users to and from the database.
Now from what I've been reading, to connect to this type of database, I have to use the driver called "PDO_dblib".
I have installed this bundle as it seems to be the only one thats still active, don't quote me on that.
https://github.com/realestateconz/MssqlBundle
Now, I installed this in my vendor folder, is this the correct place to store it? Like so:
Project/
app/
src/
vendor/
realestate/
Of course i added the bundle to the AppKernel like so:
new Realestate\MssqlBundle\RealestateMssqlBundle();
and last but not least here is what i have in my config file:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
dbname: %database_prefix%%database_name%
user: %database_user%
password: %database_password%
so what im thinking im doing here is telling doctrine to use this driver? dont see what else it could be.
I have also declared my parameters.yml for the connection settings.
PS: I am doing my Dev on Linux Mint!
before i tried the steps of this bundle i also ran throught this websites steps: https://dunglas.fr/2014/01/connection-to-a-ms-sql-server-from-symfony-doctrine-on-mac-or-linux/
But again it was throwing errors, I'll post the errors I get down below!
So for the record I have installed freetds and php5-sybase.
The errors that I am getting are such:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: class_implements(): Class Realestate\MssqlBundle\Driver\PDODlib\Driver does not exist and could not be loaded
and also this when i try to do a:
php app/console doctrine:database:create
(I do have my entity set up)
but i get the following error from the command of create:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
I have been trying to get this working for the past few days and any help would be fantastic! Any more information needed feel free to ask of course!
According your error code:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
It seems that you miss configuring the parameter “database_prefix”.
If the tables in your database have the unitized prefix e.g. “core_user”,”core_tasks”… You can configure this parameter in the file parameters.yml if not, you can just remove the parameter % database_prefix % in the dbname line in file config.yml.
Here are my code snippets for your reference:
Config.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
parameters.yml:
parameters:
database_host: {your_sql_server_name}.database.windows.net
database_port: 1433
database_name: {database_name}
database_user: {username}
database_password: {password}
And the test query in controller:
$conn = $this->get('database_connection');
$data = $conn->fetchAll('SELECT * FROM Testtable');
var_dump($data);

Is it possible to use Symfony2/Doctrine form validation without a relational database?

I have the following controller action (much simplified):
public function createAction()
{
$request = $this->getRequest();
$form = $this->createForm(
new DashboardType(),
new Dashboard()
);
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$dm = $this->get('doctrine_mongodb')
->getManager();
$dm->persist($dashboard);
$dm->flush();
}
}
}
Where dashboard is a Mongo Document object rather than an Entity. This code fails on the $form->bind line with the following DBAL error:
PDOException: could not find driver
I'm assuming this is due to the fact I don't have a relational database set up in my parameters.yml file. All I need for this app is Mongo which is run via Docker.
Is it possible to run Symfony and Doctrine with features like form validation if no database is installed? It seems a bit pointless to have to dockerise and manage a MySQL instance that won't be used purely to satisfy the Symfony requirements.
Is there a way around this?
Update
This isn't a driver issue as I can read and write fine to Mongo without form valiation. Edited response from modules:
$ php -m
[PHP Modules]
mongo
PDO
Solution
By default, Symfony adds the following to your config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
This somehow affects the form validators. By deleting that section and just having the doctrine mongo config, the errors are fixed.
You should configure Symfony to use MongoDB as persistence system.
You can find the setup process on the DoctrineMongoDBBundle of the Symfony doc :
http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

Symfony2 + Propel: no connection information

I'm trying to connect propel and symfony2 together, but the only thing that I get is this exception:
No connection information in your runtime configuration file for datasource [symfony]
I used the propel bundle which I added to the composer, and I edited the kernel to include the bundle.
My config.yml propel conf looks like that:
propel:
dbal:
driver: mysql
user: root
password: null
dsn: mysql:host=localhost;dbname=symfony;charset=UTF8
options: {}
attributes: {}
What can I be missing? I tried googling, but nothing really solves the issue.
Okay, I solved the issue.
The problem lied in the config.yml file, where the propel dbal data should be stored. I initially put it the way they describe it in the manual, like that:
propel:
dbal:
driver: "%database_driver%"
user: "%database_user%"
password: "%database_password%"
dsn: "%database_driver%:host=%database_host%;dbname=%database_name%;charset=%database_charset%"
According to the manual on both the propel and symfony's sites, it is enough. However, this doesn't seem to be the case for some reason yet unknown to me. What I had to do was to explicitly state the connection and name it precisely - symfony, like that:
propel
dbal:
default_connection: symfony
connections:
symfony:
driver: %database_driver%
user: %database_user%
password: %database_password%
dsn: %database_driver%:host=%database_host%;dbname=%database_name%;charset=UTF8
This way it worked. Sucks to be a junior developer sometimes.

Categories