Symfony 5 Cannot autowire argument $Driver for a dynamic database connection - php

I am currently facing a problem, I explain myself. For my project I'm trying to make a dynamic connection to a database (I have 2 virtual machines with a different IP but with the same identifiers and tables with MSSQL database engine (SQLSRV)).
I try something like this ->
use Doctrine\DBAL\Driver\SQLSrv\Driver;
/**
* #Route("/testconnection", name="test_connect")
*/
public function testConnection(Driver $Driver){
$connectionParams = array(
'dbname' => 'job',
'user' => 'sa',
'password' => 'Lasernet#2020',
'host' => '192.168.1.34',
'driver' => 'pdo_sqlsrv',
);
$conn = $Driver->connect($connectionParams);
dd($conn);
}
Error message
Cannot autowire argument $Driver of "App\Controller\HomeController:testConnection()": it references class "Doctrine\DBAL\Driver\SQLSrv\Driver" but no such services exists.
Error message
But the problem is that Symfony sends me back an error that I find hard to solve/understand.
If someone has a solution to my problem/success to make a dynamic connection to databases.
If you need more information tell me.

The error message says it. No such service exists.
You must define the Doctrine\DBAL\Driver\SQLSrv\Driver class as a symfony service in your public/services.yaml to autowire it.
Update your public/services.yaml with:
services:
Doctrine\DBAL\Driver\SQLSrv\Driver:
autowire: true
But why you will autowire this class? The same behaviour you can get with
public function testConnection(){
$Driver = new Driver();
$connectionParams = array(
'dbname' => 'job',
'user' => 'sa',
'password' => 'Lasernet#2020',
'host' => '192.168.1.34',
'driver' => 'pdo_sqlsrv',
);
$conn = $Driver->connect($connectionParams);
dd($conn);
}
And if you have no really dynamic values in your connection params like $connectionParams['dbname'] = 'sqldb'.$i, better to use the doctrine dbal config and get the connection with $this->getDoctrine()->getConnection('name'); in your controller.
Symfony Docs

Related

How can I pass a PDO object to an Illuminate Capsule Manager

I'm using Cartalyst's Sentinel user package as part of a user login library w/ a gui that I'm making. Sentinel uses Illuminate for the database.
So to set up the connection I have to do something like:
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($dbInfo = [
'driver' => $package->get('DB.driver'),
'host' => $package->get('DB.host'),
'charset' => $package->get('DB.charset'),
'collation' => $package->get('DB.collation'),
'database' => $package->get('DB.database'),
'username' => $package->get('DB.user'),
'password' => $package->get('DB.password'),
]);
$capsule->bootEloquent()
Where $package is my own thing & is pretty self explanator. Just returns values for keys here.
Instead of using $capsule->addConnection($arrayOfLoginInfo), I'd like to pass a previously instantiated PDO object. Something like:
$pdo = new \PDO($dsn, $username, $password);
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addPdoConnection($pdo);
$capsule->bootEloquent()
But the capsule manager does not appear to have such a method.
I've never used Illuminate before, but I did some research a couple weeks ago & wasn't able to find a solution.

Correct way to edit a database service after first initiation

I use two different database services in my Phalcon application.
db: Global database with system-wide data, including accounts and users
db_data: This database hosts customer-specific data. The database-name is different for each customer/account. This is determined when the customer sign in.
This works in the web application, when I have one account active. Now I have a cronjob, that is going to loop through a table in the global "db" database. And after it is going to connect to the specific "db_data" database.
This works for the first account, but after this it will not connect to the new database. It still use the first initiated database.
The db_data service is a shared service in the services.php:
$di->set('db_data', function () use ($config, $di) {
if(!$di->getCore()->getAccount()) {
throw new \MyNamespace\Exception(_('Account is not set. Can not load account database.'));
}
$eventsManager = $di->getShared('eventsManager');
$dbListener = new \MyNamespace\Module\Core\Helper\Model\DatabaseListener();
$eventsManager->attach('db_data', $dbListener);
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->data_dbname_prefix.$di->getCore()->getAccount()->id,
'name' => 'data',
));
$connection->setEventsManager($eventsManager);
return $connection;
}, true);
In the first model in "db" i have the following in the initialize() function:
$this->setConnectionService('db');
In the second model in "db_data" i have the following in the initialize() function:
$this->setConnectionService('db_data');
Here is an example of the cron PHP-file:
$screens = \MyNamespace\Module\DigitalSignage\Model\Screen::find(array(
'conditions' => 'deleted_at IS NULL',
));
foreach($screens as $screen) {
$console->getDi()->getCore()->setAccount(\MyNamespace\Module\Core\Model\Account::findFirst('id='.$screen->account_id));
$campaign = \MyNamespace\Module\DigitalSignage\Model\Campaign::findFirst(array(
'conditions' => 'id = :id: AND account_id = :account_id: AND deleted_at IS NULL',
'bind' => array(
'id' => $screen->digitalsignage_campaign_id,
'account_id' => $console->getDi()->getCore()->getAccount()->id,
),
));
var_dump($campaign);
}
What is the correct way to change the database service parameteres after first initiation?
Phalcon version: 3.2.2 PHP version: 7.0.22

ZF2 Zend\Log + Doctrine2

I do not know how to configure Zend \ Log with Doctrine2. Only allows you to write directly to the database via a connection adapter or write to a file.
May be it's too late to answer this question but better late than never.
I've found a good post which explains how to create a basic SQL Logger for ZF2 and Doctrine.
The approach is pretty simple :
1. Creating Logger class : Create the following class in your Module/Application/Log folder :
<?php
namespace Application\Log;
use Zend\Log\Logger;
use Doctrine\DBAL\Logging\DebugStack;
class SqlLogger extends DebugStack
{
protected $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function stopQuery()
{
parent::stopQuery();
$q = $this->queries[$this->currentQuery];
$message = "Executed Query: " . print_r($q, true);
$this->logger->info($message);
}
}
The stopQuery() function which is called by Doctrine when it finiches sending the query to the database server, is
overrided so that it could write the current query to the Logger object.
2. Configuring the Logger : Add the following code in your config/autoload/global.php file, to make
the Logger accessible to the Service Manager using the name my_sql_logger :
'service_manager' => array(
'factories' => array(
'my_sql_logger' => function($sm) {
$log = new \Zend\Log\Logger();
$writer = new \Zend\Log\Writer\Stream('./data/logs/sql.log');
$log->addWriter($writer);
$sqllog = new \Application\Log\SqlLogger($log);
return $sqllog;
},
)
),
The Logger will write data to the data/logs/sql.log file. So, make sure that data/logs folder exists in your
application root directory.
3. Configuring Doctrine : Now you need to tell Doctrine to use the created Logger. Just add the following code
to your Doctrine configuration :
return array(
'doctrine' => array(
/*--------Add this code------------*/
'sql_logger_collector' => array(
'orm_default' => array(
'sql_logger' => 'my_sql_logger',
),
),
/*---------------------------------*/
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'dbname',
),
),
),
),
);
With the above configuration of Zend\Log and Doctrine2, you'll get all the query data logged in the
data/log/sql.log file.
Please see this Sql Logger for ZF2 and Doctrine for more details.

Problems connecting to db using Zend db as standalone

I am migrating from ZF1 Zend_db to ZF2. I have problems connecting to the db and making a simple query. Appreciate if someone can guide me along. Below is my code.
This is how i connect to db
use Zend\Db\Adapter\Adapter;
$dbo = new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo_mysql',
'database' => DB_PREFIX.DB_NAME,
'username' => DB_USER,
'password' => DB_PW
));
Zend_Registry::set('db', $dbo);
This isan example how i use it.
$this->dbo = Zend_Registry::get('db');
function createData($message,$tags,$userid,$imgsrc){
$data= array(
'message' => $message,
'tags' => $tags,
'imgsrc' => $imgsrc,
'createdtimestamp'=>new Zend_Db_Expr('NOW()'),
'userid' => $userid);
$this->dbo->insert('mydata', $data);
return $this->dbo->lastInsertId();
}
I have an error. That is $dbo does not have select(),insert() methods etc. I could did it in ZF1 zend db.
Example of error message i received:
Fatal error: Call to undefined method
Zend\Db\Adapter\Adapter::select() in
You seem to think that ZF2 and ZF1 are (and should be) class-compatible. The problem is, they're not - at least in regards to Zend\Db\Adapter. That one in ZF2 is much more concise: while it has query method, its main responsibility is abstracting the DB connection itself.
I suppose what you're looking for is located in Zend\Db\Sql area. Assuming you have your DB connection in $dbo, you can create an Sql object based on it:
use Zend\Db\Sql as Sql;
$sql = new Sql\Sql($dbo);
... then create another object (of Zend\Db\Sql\Insert class):
$insert = $sql->insert('mydata');
$insert->values([
'message' => $message,
'tags' => $tags,
'imgsrc' => $imgsrc,
'createdtimestamp'=>new Sql\Expression('NOW()'),
'userid' => $userid
]);
... then use this object in query:
$insertString = $sql->getSqlStringForSqlObject($insert);
$results = $dbo->query($insertString, Adapter::QUERY_MODE_EXECUTE);

Zend Framework Firebird Example

I am new to Zend. I'm using Zend 1.11 and trying to successfully connect to a Firebird database. As far as I can tell I have all the php_interbase stuff enabled. I see the ZendX firebird adapter, but I still get this message
Warning: include_once(Zend\Db\Adapter\Php\Firebird.php)
[function.include-once]: failed to open stream: No such file or
directory in C:\wamp\bin\php\Zend_Framework\library\Zend\Loader.php on
line 146
As if it has no idea what adapter I'm speaking of.
I'm using this in my boot strap
protected function _initDb()
{
$this->bootstrap('config');
$config = $this->getResource('config');
$db = Zend_Db::factory('Php_Firebird', array(
'host' => $config->Database->Server,
'username' => $config->Database->Username,
'password' => $config->Database->Password,
'dbname' => $config->Database->DBName
));
return $db;
}
I'm assuming this has something to do with the fact this is ZendX stuff not Zend\db stuff but I cannot find an example of it. Or from the factory function using a ZendX adapter. I tried to use 'Php_Interbase' but that was not found either (and I dont see it in the folders anyway). And I tried Pdo_Firebird as well which of course didnt work.
Has someone done this that can point me to what I'm doing wrong?
Thanks
You just need to add adapterNamespace to the configuration array you are passing to the factory. See the 3rd example here, also remove 'PHP_' from the adapter name, so your call to the factory should look like this:-
$db = Zend_Db::factory('Firebird', array(
'host' => $config->Database->Server,
'username' => $config->Database->Username,
'password' => $config->Database->Password,
'dbname' => $config->Database->DBName,
'adapterNamespace' => 'ZendX_Db_Adapter'
));
Try something like
$db = new ZendX_Db_Adapter_Firebird(array(
//config part
));

Categories