when i try connecting to my sql server database from my zf2 application as shown below,
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'sqlsrv:dbname=album;hostname=192.168.0.20',
'username' => 'user',
'password' => 'pass',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
I get this error,
File:
/usr/local/zend/apache2/htdocs/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Connection.php:289
Message:
Connect Error: could not find driver
Am i missing something here? Or rather this does not work at all on the linux oriented machines ?
Try taking out this line: "// PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''".
Your connection say you are trying to connect to MSSQL and not MySQL.
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'sqlsrv:dbname=album;hostname=192.168.0.20',
'username' => 'user',
'password' => 'pass',
'driver_options' => array(
// PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
I always prep it like this without trouble, using dist files to set the right connection params as you work through deployment:
local config file
'database' => array(
'connection' => array(
'mysql_master' => array(
'driver' => 'Pdo_Mysql',
'host' => 'host',
'dbname' => 'analytics',
'username' => 'user',
'password' => 'pass',
'charset' => 'UTF8',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
),
),
),
),
Then in my Module.php, I define such a factory (in the getServiceConfig array):
'Zend\Db\Adapter\Adapter' => function ($sm){
$config = $sm->get('config');
$dbParams = $config['database']['config']['mysql_master'];
$adapter = new \Zend\Db\Adapter\Adapter($dbParams);
return $adapter;
},
Hope this helps. If this doesn't work, something else is going on.. let me know, I can help.
Cheers.
Alex
The problem is that you don't have the necessary driver installed. I had the same problem with postgres, and this solved it for me:
apt-get install php5-pgsql
However, I am not 100% sure what driver must be installed to make Sql Server work. I did read that it's no longer supported. See this: PHP PDO to MS SQL Server on Ubuntu Server. In a nutshell, your best bet is to rather use:
sudo apt-get install php5-sybase
and then connection string:
"dblib:host=sever;dbname=dbname"
From what I gather, that should connect you to a Sql Server database, but again, I can't gaurantee this because I don't have a setup to test it with. Hope that helps...
I hope this helps you some... I'm currently following along to the Zend Framework 2 skeleton application guide. My database parameters needed to be passed in the following way:
'db' => array(
'driver' => 'SQLSRV',
'hostname' => 'SqlServerName',
'Database' => 'SqlDatabaseName',
'uid' => 'UsertoConnectwith',
'pwd' => 'Passwordofusertoconnectwith'
),
Related
Initially, I started to develop a web application using Zend Framework 2 and Doctrine2 with MySQL but the customer wants that the database would be Oracle. So, I have to change the database from MySQL to Oracle.
To do that, I have modified my doctrine.local.php file from this version
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'user_gnsys',
'password' => '******',
'dbname' => 'gnsys',
),
'doctrine_type_mappings' => array(
'enum'=>'string'
)
),
)
)
);
To this other version ...
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\OCI8\Driver',
'params' => array(
'host' => 'localhost',
'port' => '1521',
'dbname' => 'GNSYS',
'driver' => 'oci8',
'user' => 'c##gnsys',
'password' => '********',
'servicename' => 'demo31',
),
'doctrine_type_mappings' => array(
'enum'=>'string'
)
),
)
)
);
Next, I try to validate my schema using the next command:
josecarlos#R2D2:~/Workspace/Web/gnsys$ php public/index.php orm:validate-schema
And I've got the next exit ...
How can I remove all these "Notice" that I've got on the exit? Have I forget to update another file?
I fixed the problem installing oci8 library.
I have 3 type of databases:
Authen DB (fixed address and fixed schema)
Config DB (fixed address and fixed schema)
Service DBs (dynamic address and dynamic schema based on each service)
After users logined and verified via Authen DB.
Based on the information store in Config DB, all actions in ZF2 application relate to the service should be done on the correlative Service DBs.
Does ZF2 support this case? How can I solve this?
Below codes are my global.php and local.php.
global.php
return array(
'db' => array(
// primary database
'driver' => 'Pdo',
'dsn' => 'mysql:host=xxx.xxx.xxx.xxx;dbname=db_authen',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
),
// other database
'adapters' => array(
'db_config' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:host=yyy.yyy.yyy.yyy;dbname=db_config',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
),
),
),
),
'service_manager' => array(
// primary database
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory'
),
// other database
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstractServiceFactory',
),
),
);
local.php
return array(
'db' => array(
// primary database
'username' => '*****',
'password' => '*****',
// other database
'adapters' => array(
'db_config' => array(
'username' => '*****',
'password' => '*****',
),
),
),
);
Thanks,
After you do your authentication against the (statically configured) Auth DB and consult the (statically configured) Config DB for the dynamic information you need for the Service DB, you could probably instantiate yourself the correct DB-adapter for the Service DB, using something like:
// Config from the Config DB, packaged into an array with keys that
// are expected by \Zend\Db\Adapter\Adapter
$config = [
'driver' => 'Pdo_Mysql', // for example
'user' => 'my-dynamically-obtained-user',
'password' => 'my-dynamically-obtained-password',
'database' => 'my-dynamically-obtained-db-name',
// etc
];
$adapter = new \Zend\Db\Adapter\Adapter($config);
// Now use the $adapter to build queries
$statement = $adapter->query('SELECT * FROM `mytable`');
$results = $statement->execute();
// iterate over the results, etc.
Alternatively, you could feed the $adapter into a model object you create that hides the db-specific query details from the consumer.
See ZF2 docs for \Zend\Db\Adapter\Adapter
On ZF2 I need to use a persistent MySQL connection and reconnect and the MySQL is gone away.
But I can not figure out where I should activate the MYSQL_OPT_RECONNECT parameter.
My db adapters are definied as follow:
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=DB_NAME;host=HOST',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
),
'username' => 'LOGIN',
'password' => 'PWD',
),
I have tried things like :
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=DB_NAME;host=HOST',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
'AUTO_RECONNECT_ON_UNSERIALIZE' => 1,
),
'options' => array(
'AUTO_RECONNECT_ON_UNSERIALIZE' => 1,
),
'username' => 'LOGIN',
'password' => 'PWD',
),
and nothing works.
How to make this mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect);
happen somewhere ?
Ok, the answer is :
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=DB_NAME;host=HOST',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
),
'options' => array(
PDO::ATTR_PERSISTENT => true',
),
'username' => 'LOGIN',
'password' => 'PWD',
),
But the use of persistant connection might not be a good fit for every one so you can also catch any "Mysql Server Gone Away Error" and proceed to a reconnect via :
$this->tableGateway->getAdapter()->getDriver()->getConnection()->disconnect();
$this->tableGateway->getAdapter()->getDriver()->registerConnection($this->getAdapter()->getDriver()->getConnection()->connect());
if($this->tableGateway->getAdapter()->getDriver()->getConnection()->isConnected()) {
return true;
}
Is it possible to make an SSL encrypted connection via ZF2 to my MySql Server?
And if yes, how is it possible?
I can't find anything for ZF2 PDO SSL connection on the web.
return array(
'db' => array(
'adapters' => array(
// The first (default) database connection
'zf2' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=zf2;host=sandbox-db-vm',
'username' => 'root',
'password' => 'password',
),
// Now the second database connection
'zf2ssl' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=zf2;host=sandbox-db-vm',
'username' => 'ssl_user',
'password' => 'ssl_test',
PDO::MYSQL_ATTR_SSL_KEY => '/etc/mysql-ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql-ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/etc/mysql-ssl/ca-cert.pem'
),
),
),
'service_manager' => array(
// Let's make sure our adapters get instantiated
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstractServiceFactory',
),
),
);
Trying to wrap my head around the new concepts of Zend Framework 2.0.
I'm trying to connect to a database, and to get that connection in a controller or model.
Nothing fancy, just the pure connection to run queries against.
So this is my current code:
//module.config.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=DBNAME;host=HOSTNAME,
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
What am I doing wrong?
Create db.local.php in your ./config/autoload folder and add the following content
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zenBlog;host=localhost',
'username' =>'root',
'password' =>'',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),);
in your controller $this->getServiceLocator()->get('db'); to access to database.
This is how my local.php in config\autoload\local.php looks like.
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=<dbname>;host=localhost',
'username' => 'root',
'password' => <your password here>,
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'aliases' => array(
'adapter' => 'Zend\Db\Adapter\Adapter',
),
),);
Now use this to create a database adapter:
$adapter = $this->getServiceLocator()->get('adapter');
Create a sql statetment and put in variable $sql.
Now do this:
$statement = $adapter->createStatement($sql);
$result = $statement->execute();
Hope this helps.