Zend/Db not working in Zend Framework 3? - php

How to connect mysql database using ZF3 db factory method.
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => MASTER_HOST,
'username' => MASTER_USER,
'password' => MASTER_PASSWORD,
'dbname' => DB_ADMIN,
'port' => MASTER_HOST_PORT,
);

In ZF3 (or ZF2), you must use Zend\Db\Adapter\Adapter
$config = array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=db;host=localhost',
'username' => 'root',
'password' => 'root',
);
// or
$config = $serviceLocator->get('Config')['db'];
$db = new \Zend\Db\Adapter\Adapter($config);
Documentation:
https://framework.zend.com/manual/2.4/en/modules/zend.db.adapter.html
https://framework.zend.com/manual/2.4/en/tutorials/tutorial.dbadapter.html

Related

Cannot connect to Google Cloud SQL through Google App Engine with Pdo_Mysql in ZF2

I have been trying to solve this issue already for a few days without any results. When using default php pdo object I can connect to the database:
$db = new \PDO('mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'root', // username
'' // password
);
But when trying to connect with ZF2 adapter the connection just times out.
'db' => array(
'driver' => 'Pdo_Mysql',
'database' => 'test',
'username' => 'root',
'unix_socket' => '/cloudsql/project-id:database-instance',
),
I am quite sure that the problem is somehow with the unix_socket as I can connect to the Cloud SQL server from my localhost directly without socket:
'db' => array(
'driver' => 'Pdo_Mysql',
'host' => 'xxx.xxx.xxx.xxx',
'database' => 'test',
'username' => 'user',
'password' => 'password',
),
What I am missing?
unix_socket isn't a recognized option for the pdo driver (https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Pdo/Connection.php#L164). Try this instead
'db' => array(
'dsn' => 'mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'username' => 'user',
'password' => 'password',
)

Connection timeout with Zend_Db::factory using PDO_MYSQL

What is the proper (or better) way to set connection timeout with PHP code, when using Zend_Db::factory with PDO_MYSQL.
I have:
$params = array (
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'UTF8',
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
a.)
$db->getConnection()->setAttribute(PDO::ATTR_TIMEOUT, 600);
b.)
$sql = "SET SESSION wait_timeout = 600";
$db->getConnection()->query($sql);
Reduce the connection timeout to 3 seconds:
$params = array (
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'UTF8',
'driver_options' => [
\PDO::ATTR_TIMEOUT => 3
]
);
$db = Zend_Db::factory('PDO_MYSQL', $params);

How I can use the Zend framework database classes in core php project --

How I can use the Zend database classes in core php project --
I downloaded ZendFramework-2.3.1
extracted Zend folder from its library folder to my project library folder --
projectdir/
library
-- Zend
index.php
In index.php I am using this code --
use library\Zend\Db\Adapter\Adapter ;
// use Zend\Db\Adapter\Adapter ; also tried this line
$adapter = new Adapter(array(
'driver' => 'Pdo_Sqlite',
'hostname'=>'localhost',
'database' => 'pub_crawl',
'username' => 'root',
'password' => 'Micro123#'
)
);
I have also tried this --
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Pdo_Sqlite',
'hostname'=>'localhost',
'database' => 'pub_crawl',
'username' => 'root',
'password' => 'Micro123#'
));
$adapter = new \Zend\Db\Adapter\Adapter(array(
'driver' => 'Pdo_Sqlite',
'hostname'=>'localhost',
'database' => 'pub_crawl',
'username' => 'root',
'password' => 'Micro123#'
));
$adapter = new library\Zend\Db\Adapter\Adapter(array(
'driver' => 'Pdo_Sqlite',
'hostname'=>'localhost',
'database' => 'pub_crawl',
'username' => 'root',
'password' => 'Micro123#'
));
$adapter = new Adapter(array(
'driver' => 'Pdo_Sqlite',
'hostname'=>'localhost',
'database' => 'pub_crawl',
'username' => 'root',
'password' => 'Micro123#'
)
);
print_r($adapter) ;
Fatal error: Class 'Zend\Db\Adapter\Adapter not found in /var/www/pubcrawl/index.php on line 25
Any help would be appreciated.
Thanks,
Abhishek
I solved my prob. by doing this with help of references provided above by Thiago França in comments --
and was able to get connection of database using Pdo_Mysql as driver and data from tables.
just putted Zend folder at root inside project directory --
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister' => true));
$loader->setFallbackAutoloader(true);
// Register with spl_autoload:
$loader->register();
use Zend\Db\Adapter\Adapter ;
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Pdo_Mysql',
'hostname'=>'localhost',
'database' => 'pub_crawl',
'username' => 'root',
'password' => 'Micro123#'
));
$optionalParameters = array();
$sql = 'SELECT * FROM pubs' ;
$statement = $adapter->createStatement($sql, $optionalParameters);
$result = $statement->execute();
$row = $result->current() ; // for getting first record
hope it may help someone.

Add connection to DBAL dynamically in Silex

I am writing a PHP application using the Silex framework. I'm using the Doctrine Service Provider, and I can open a connection normally as this:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'localhost' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8',
)
),
));
That works perfectly. What I want now is to add another database connection afterwards in my code. I know I can do it adding another element to dbs.options, but I want to do it afterwards, in the controllers (as different controllers will use different database connections).
Is that possible? I guess I could use something like DriverManager::getConnection($options, $config, $manager); but there's probably a better way to do it.
Thanks!
$conn = DriverManager::getConnection($params, $config);
this is original code to generate new connection, so what you wrote is ok
Link: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html
You can configure multiple db connections using the DoctrineServiceProvider bundled with Silex.
Replace the db.options with an array of configurations where keys are connection names and values configuration options.
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'mysql_read' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_read.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
'mysql_write' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_write.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
),
));
Access multiple connections in your controllers:
$app->get('/blog/{id}', function ($id) use ($app) {
$sql = "SELECT * FROM posts WHERE id = ?";
$post = $app['dbs']['mysql_read']->fetchAssoc($sql, array((int) $id));
$sql = "UPDATE posts SET value = ? WHERE id = ?";
$app['dbs']['mysql_write']->executeUpdate($sql, array('newValue', (int) $id));
return "<h1>{$post['title']}</h1>".
"<p>{$post['body']}</p>";
});
Source: http://silex.sensiolabs.org/doc/providers/doctrine.html

Doctrine2: additional connection options

Is there a way to add some additional parameters to the database connection in Doctrine2?
For example I need to enable network communication compression.
$DoctrineConnectionOptions = array(
'driver' => 'pdo_mysql',
'dbname' => 'db',
'user' => 'root',
'password' => 'root',
'host' => 'localhost',
/*
additional_options => array(
PDO::MYSQL_ATTR_COMPRESS => true
)
*/
);
$em = \Doctrine\ORM\EntityManager::create($DoctrineConnectionOptions, $DoctrineConfig);
Check EntityManager::create() source code. You can directly pass customly made PDO object as a connection option ('pdo' key).

Categories