That is my local.php config:
'db' => array(
'driver' => 'pdo_mysql',
'dns' => 'mysql:dbname=xxxx;host=localhost;',
'username' => 'xxxx',
'password' => 'xxxx',
'port' => 3306,
'charset' => 'UTF8',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
)
)
And that is my authentication code:
$authAdapter = new AuthAdapter($sm->get('Zend\Db\Adapter\Adapter'));
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment("SHA2(CONCAT({$salt}, ?, salt), '512') AND active = 1");
$authAdapter->setIdentity($formData['username'])
->setCredential($formData['username']);
But when I try to submit login form, zend framework throw this errors:
File: /var/www/app/vendor/zendframework/zend-authentication/src/Adapter/DbTable/AbstractAdapter.php:331
Message: The supplied parameters to DbTable failed to produce a valid sql statement, please check table and column names for validity.
File: /var/www/html/bookingSystemRewrite/vendor/zendframework/zend-db/src/Adapter/Driver/Pdo/Statement.php:244
Message:Statement could not be executed (3D000 - 1046 - No database selected)
File:/var/www/html/bookingSystemRewrite/vendor/zendframework/zend-db/src/Adapter/Driver/Pdo/Statement.php:239
Message:SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
When I debug Pdo Driver Adapter I found where is my problem.
The first problem was in that order:
'dns' => 'mysql:dbname=xxxx;host=localhost;'
it must be so :
'dsn' => 'mysql:dbname=xxxx;host=localhost;'
And the second problem was in that order:
->setCredentialTreatment("SHA2(CONCAT({$salt}, ?, salt), '512') AND active = 1");
it must be so:
->setCredentialTreatment("SHA2(CONCAT('{$salt}', ?, salt), '512') AND active = 1");
Related
I am writing below query after connecting sql server in cakephp 2.x.
$this->User->find('first', array('conditions' => array('User.USER_ID' => 1)));
This produces following query
SELECT TOP 1 [User].[USER_ID] AS [User__USER_ID], [User].[USER_NAME] AS [User__USER_NAME]
FROM [ec_user_details] AS [User]
WHERE [User].[USER_ID] = 1
The above query through following error
SQLSTATE[42S02]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'ec_user_details'.
How can I create the following query by ORM in cakephp
SELECT TOP 1 [User].[USER_ID] AS [User__USER_ID], [User].[USER_NAME] AS [User__USER_NAME]
FROM [**DB_NAME**].[ec_user_details] AS [User]
WHERE [User].[USER_ID] = 1
Please help
After spending lots of time I found the solutions. Add schema in the db config
public $default = array(
'host' => '127.0.0.1',
'login' => 'username',
'password' => 'paasword',
'database' => 'db_name',
'datasource' => 'Database/Sqlserver',
'persistent' => false,
'prefix' => '',
'encoding' => PDO::SQLSRV_ENCODING_UTF8,
'schema'=>'db_name'
);
I have following in the configuration of a Zend project
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=food;host=localhost',
'username' => 'root',
'password' => '',
'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 am trying to get DB adapter
($adapter = $adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
( $adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
in a controller but is always stuck with.
class: "Zend\ServiceManager\Exception\ServiceNotCreatedException"
file:
" /path/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php"
line: 909 message: "An exception was raised while creating
"Zend\Db\Adapter\Adapter"; no instance returned"
An exception was raised while creating "Zend\Db\Adapter\Adapter"; no instance returned
This could be your problem. The __construct method can throw an InvalidArgumentException(docs).
You should try to create an instance of Zend\Db\Adapter\Adapter on your own and see if an exception is thrown.
I'm trying to create a ZF2 application with multiple databases. Based on a user, the database should be dynamically set.
Right now I've the following:
database.local.php
return array(
'db' => array(
'adapters' => array (
'master_db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=master_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD'
),
'tentant_db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=tenant_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD'
),
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstractServiceFactory',
)
),
);
For test purposes I've created a form that has a method to fetch some data and put it in a select box. The code to get the database connection is shown in the code below.
MyController.php (in some module)
//... some code
public function someAction(){
$dbAdapter = $this->getServiceLocator()->get('tentant_db');
$form = new AddEolConnectorForm($dbAdapter);
$viewModel = new ViewModel(array(
'form' => $form
));
return $viewModel;
}
//... some more code
My question is, how can I dynamically set the dbname for the tentant_db adapter in my controller (or module)?
Thanks for your help.
The config merge event is one of zend newer event's I believe. It triggers when zend is mergin the config array's which is perfect for the problem you are facing since you can override some array key's dynamically.
public function onMergeConfig(ModuleEvent $e)
{
$configListener = $e->getConfigListener();
$config = $configListener->getMergedConfig(false);
// I'm actually not sure if you have the route match here otherwise you may have to
// use some other method to retrieve the url.
$match = $e->getRouteMatch();
switch ($match) {
case 'first-dependency':
$config['db']['adapter'] => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=master_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
break;
case 'second-dependency':
$config['db']['adapter'] => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=tenant_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
break;
// Pass the changed configuration back to the listener:
$configListener->setMergedConfig($config);
}
Based on the above answer I've created to following:
Module.php
class Module implements AutoloaderProviderInterface
{
public function init(ModuleManager $moduleManager)
{
$events = $moduleManager->getEventManager();
// Registering a listener at default priority, 1, which will trigger
// after the ConfigListener merges config.
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig'));
}
public function onMergeConfig(ModuleEvent $e)
{
$db = $this->getTentantDb();
$configListener = $e->getConfigListener();
$config = $configListener->getMergedConfig(false);
$config['db']['adapters']['tenant_db']['dsn'] = 'mysql:dbname='. $db .';host=localhost';
$configListener->setMergedConfig($config);
}
// Some more code
public function getTenantDb(){
$tenant_db = 'tenant_12345'
return $tenant_db;
}
}
I don't know if it is the best solution, but the above code is working. I think the next steps should be to put the code in a generic module or something so I can access it from all my modules.
I'm sorry I'm completely new to Zend Framework 2 with some tutorials I'm trying to connect my DB connection as follows,
Created a file in
xampp\htdocs\articlemanager\application\configs\autoload\global.php
Inserted the following Zend DB connection code to global.php
<?php
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
'db' => array(
'driver' => 'PDO_MYSQL',
'dsn' => 'mysql:dbname=articlemanager;host=localhost',
'username' => 'root',
'password' => '',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
);
and In the Indexcontroller (\xampp\htdocs\articlemanager\application\controllers\IndexController.php) tested adding $this->db = $this->getServiceLocator()->get('db'); in the indexAction as follows
public function indexAction()
{
$this->db = $this->getServiceLocator()->get('db');
}
When I refresh page it display as
An error occurred
Application error
Can I know what I missed here?
Also I would like to know My Zend Library is in the \xampp\php\Zend and My global.php file in the xampp\htdocs\articlemanager\application\configs\autoload\global.php is it OK?
Why are you using an Alias on 'Db' ?
Try this, your driver name is different from mine.
In addition, please move your username and password to local.php.. so they do not persist in Git projects
global.php
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=articlemanager;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
local.php example
<?php
return array(
'db' => array(
'username' => 'DB_USERNAME',
'password' => 'DB_PASSWORD',
),
);
database connection problems can be a number of different issues that are not necessarily obvious from looking at the creds / db config info.
have you taken a look at any logs? my application has several different logs setup - PHP error, access log, mysql error log, etc. - depending on your application, you may not have this many as discretely defined, but checking any logs you do have will give you a whole lot more information than just "An error has occurred" :)
Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message
'No adapter found for Zend_Session_SaveHandler_DbTable' in
C:\wamp\www\hol\library\Zend\Db\Table\Abstract.php on line 755 ( ! )
Zend_Db_Table_Exception: No adapter found for
Zend_Session_SaveHandler_DbTable in
C:\wamp\www\hol\library\Zend\Db\Table\Abstract.php on line 755 Call
Stack
Time Memory Function Location 1 0.0005 373664 {main}( ) ..\init.php:0
2 0.0325 2749720 Zend_Session_SaveHandler_DbTable->__construct(
) ..\init.php:40 3 0.0325 2750168 Zend_Db_Table_Abstract->__construct(
) ..\DbTable.php:207
4 0.0325 2750480 Zend_Session_SaveHandler_DbTable->_setup(
) ..\Abstract.php:268 5 0.0325 2750480 Zend_Db_Table_Abstract->_setup(
) ..\DbTable.php:403
6 0.0325 2750480 Zend_Db_Table_Abstract->_setupDatabaseAdapter(
) ..\Abstract.php:739
Code:
//Configuration consumption
$config = new Zend_Config(require 'config.php');
//Database configuration
$db = Zend_Db::factory($config->database->adapter, array(
'host' => $config->database->params->host,
'username' => $config->database->params->username,
'password' => $config->database->params->password,
'dbname' => $config->database->params->dbname
));
$sess_config = array(
'name' => 'session',
'primary' => array(
'session_id',
'save_path',
'name',
),
'primaryAssignment' => array(
'sessionId',
'sessionSavePath',
'sessionName',
),
'modifiedColumn' => 'modified',
'dataColumn' => 'session_data',
'lifetimeColumn' => 'lifetime',
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($sess_config));
//Initialize the session
Zend_Session::start();
config
<?php
// config.php
return array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'hol'
)
),
'session' => array(
'name' => 'session',
'primary' => array(
'session_id',
'save_path',
'name',
),
'primaryAssignment' => array(
'sessionId',
'sessionSavePath',
'sessionName',
),
'modifiedColumn' => 'modified',
'dataColumn' => 'session_data',
'lifetimeColumn' => 'lifetime'
)
);
SQL:
CREATE TABLE `session` (
`session_id` char(32) NOT NULL,
`save_path` varchar(32) NOT NULL,
`name` varchar(32) NOT NULL DEFAULT '',
`modified` int,
`lifetime` int,
`session_data` text,
PRIMARY KEY (`Session_ID`, `save_path`, `name`)
);
You didn't post the contents of your config.php, but based on the error I suspect you did not specify an adapter (mysql, pdo, etc).
My config file looks something like (yaml):
database:
adapter: Pdo_Mysql
params:
host: myhost
dbname: mydb
username: myusername
password: mypassword
According to the Zend API docs, you have to specify the adapter like this:
// Set this before you make the call to setSaveHandler()
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($sess_config));
You either need to call Zend_Db_Table_Abstract::setDefaultAdapter($db) providing the $db object you created in your configuration example, as it appears you don't have a default DB adapter set up given the error, or you need to add the $db object to the $sess_config array so it gets set up as the DB adapter for Zend_Session.
Zend_Session_SaveHandler_DbTable extends Zend_Db_Table_Abstract and any options unknown to Zend_Session_SaveHandler_DbTable (e.g. database configuration options) are then passed to the constructor of Zend_Db_Table_Abstract which sets up the DbTable.
Try this:
$sess_config = array(
'db' => $db, // Pass the $db adapter as the 'db' parameter to Zend_Db_Table_Abstract
'name' => 'session',
'primary' => array(
'session_id',
'save_path',
'name',
),
'primaryAssignment' => array(
'sessionId',
'sessionSavePath',
'sessionName',
),
'modifiedColumn' => 'modified',
'dataColumn' => 'session_data',
'lifetimeColumn' => 'lifetime',
);