I am trying to use the Zend Framework without using the MVC structure, specifically the Db_Table classes.
I have created a couple of classes representing my database tables, i.e.
class DBTables_Templates extends Zend_Db_Table_Abstract
{
protected $_name = "templates";
}
When I try to instantiate this class (it is included fine), I get the following error:
Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for DBTables_Templates'
Does anyone know how I create and include the database adapter for the Db_Table classes to use?
Any pointers are greatly appreciated! I am using the latest version of ZF.
You need to create a Zend_Db_Adapter, which is the class you use to connect to the database.
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
Or you can use the factory() method to make instantiation more configurable:
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
See http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting
Then specify this adapter object to your table class. There are at least three ways to do this:
Set an application-wide default for all tables:
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Specify the adapter to the table constructor:
$table = new MyTable( array('db'=>$db) );
Store the adapter in the registry and specify it to the table or set it as default:
Zend_Registry::set('my_db', $db);
$table = new MyTable( array('db'=>'my_db') );
// alternatively:
Zend_Db_Table_Abstract::setDefaultAdapter('my_db');
See http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.constructing
Related
Since the implementation of Doctrines DBAL we now can use different data-sources/connections (Hooray!).
We'd now like to embed one of our services within TYPO3 using this new feature.
The problem now is, that the domain models in this secondary database don't match the typical Extbase schema. E.g. it does't use int IDs nor are these models nested by any kind of parent ID.
Does anyone have experiences with this use-case or has an example?
Or do I have to setup an complete parallel Doctrine ORM to handle those models?
You could try to map your database fields, like here: https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/4-use-foreign-data-sources.html
I am not sure I understood your question correctly but we had a similar case. What we did is we added the second database in the connections.
LocalConfiguration.php
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8',
'dbname' => 'default_dbname',
'driver' => 'mysqli',
'host' => 'default_dbhost',
'password' => 'default_dbpwasswd',
'user' => 'default_dbuser',
],
'Secondary' => [
'charset' => 'utf8',
'dbname' => 'secondary_dbname',
'driver' => 'mysqli',
'host' => 'secondary_dbhost',
'password' => 'secondary_dbpasswd',
'user' => 'secondary_dbuser',
],
],
],
Then added a custom class that gets the data from the Database with the Query Builder
You first have to open a connection to your database, then create a query Builder Object for that connection in the class you use to get the data.
/**
* #var Connection
*/
private $connection;
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$this->secondaryConnection = $connectionPool->getConnectionByName('Secondary');
$queryBuilder = $this->secondaryConnection->createQueryBuilder();
// now you can get the data with query statements as linked above
$statement = $queryBuilder->select('your_field')->from('your_table')->execute();
while ($row = $statement->fetch()) {
$myObjects[] = new \Vendor\Extension\Domain\Model\ExternalObject($row);
}
I cast each result row to a "ExternalObject" Model that I added, it mapped the fields as closely as possible to my conventional Extbase Objects and then was able to use them almost interchangeably.
It depends on the Data you have in your database though, and the things you want to do with them.
PostgreSQL, Oracle and many other DBMS's use SCHEMA, so, the table name is
schema_name.table_name
But CakePHP manuals not say anithing about this. What about Model, View and Controller names in the CakePHP defaults? I can use a solution like prefix, that is, where the same schema name will be used at all database operations.
PS1: please not to be confused with method Modelschema, and questions about accessing this method.
PS2: the Bill's 2006 solution is not the better one, because is not updated (I am using CakePHP2) and is not a "official cakePHP solution".
PS3: database.php have some schema attribute? What the link to CakePHP documentation?
Good news for me, there are CakePHP 2.0 documentation about SQL-Schema... No other documentation or examples, but a starting point...
In CakePHP you must define more database config.
In CakePHP 2:
set the 'schema' param to your config
create new configs for all of your schema
use the right schema in your models
For example, database conf:
public $default = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_db_user',
'password' => 'my_db_passw',
'database' => 'my_project_db',
'prefix' => '',
'encoding' => 'utf8',
'schema' => 'postgres'
);
public $other_schema = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_db_user',
'password' => 'my_db_passw',
'database' => 'my_project_db',
'prefix' => '',
'encoding' => 'utf8',
'schema' => 'other_schema'
);
If you want to use it in a model:
class AppModel extends AppModel {
public $useDbConfig = 'other_schema';
}
In CakePHP3 is the same way, just there the database is config/app.php and you must use
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
I have followed the zend instructions for implement my web Authentication using a database table with Mysqli driver configured.
When render the page, the following exceptions appears after execute authenticate method:
Zend\Db\Adapter\Exception\ErrorException
File:
C:\xampp\htdocs\pfc\vendor\ZF2\library\Zend\Db\Adapter\Driver\Mysqli\Statement.php:188
Mensaje:
Commands out of sync; you can't run this command now
It seems to be that it happen because the driver don't can execute multiples querys without free the results of the previous query first (multiquery problem).
I discovered that if changed the driver of the dbAdapter to pdo_mysql, authenticate method works ok. But I don't want use PDO drivers by performance reasons.
How I configure the Mysqli driver for can do it?
My code bellow:
$dbAdapter = new DbAdapter(array(
//'driver' => 'Mysqli', //This driver fail when call authenticate method
'driver' => 'Pdo_Mysql', //This driver works ok
'database' => 'securedraw',
'username' => 'root',
'password' => ''
));
$authAdapter = new AuthDbTableAdapter($dbAdapter);
$authAdapter->setTableName('users')
->setIdentityColumn('mail')
->setCredentialColumn('password')
->setIdentity('josep')
->setCredential('josep');
$authResult = $authAdapter->authenticate(); //Method fail
I am pretty sure what the problem is is that you are mixing up the ways execute the query. So what you have is for a PDO call and to use MySQLi you need to do
$dbAdapter = new DbAdapter(array(
'driver' => 'Mysqli', //This driver fail when call authenticate method
'database' => 'securedraw',
'username' => 'root',
'password' => ''
));
$adapter = new Zend\Db\Adapter\Adapter($configArray);
$adapter->query("QUERY HERE");
I would reference this page here https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html as I am trying to learn how to do this myself
Thanks for previous replies
How to connect mysql to zend framework. i created table in mysql. i used
$config = array(host' => 'localhost',username' => 'userName','password' => 'sqlpass',
'dbname' => 'databasename',$db = Zend_Db::factory('PDO_MYSQL', $config);
print_r($db->fetchAll("SELECT * FROM tablename")); this command to connect with database, but whenever i tried this, nothing is displayed. Is there any tutorials to connect database with zend framework. i am new to this topic. pls guide me.
try this:
$config = array('host' => 'localhost',
'username' => 'userName',
'password' => 'sqlpass',
'dbname' => 'databasename');
$ResourceDb = Zend_Db::factory('PDO_MYSQL',$config);
Zend_Db_Table_Abstract::setDefaultAdapter($ResourceDb);
and now try with fetchAll().
which error do you receive?
The following ZendCasts cast, shows a way to use doctrine 2 in a zend framework environment.
Using this configuration, how can I make the connection use a utf-8 charset so the magic of "SET NAMES 'utf8'" will happen ?
What I'm really searching for is a way to configure it using the application.ini file.
If that's not possible using this configuration, how can this be done by code ? an _initDoctrine method in the Bootstratp file ?
Thank you.
UPDATE
It appears there's a post connect event which handles this, but I don't see how can I set it up via application.ini (if possible at all).
If not, can I set it up via a bootstrap method ? Will the bootstrap method run before any other doctrine connection code run, when relying on the Bisna library ?
If you are not using Bisna, you could simply do something like the following:
Pass the config stuff directly to EntityManager's connection options
(although driverOptions is not documented)
// $options is a simple array to hold your data
$connectionOptions = array(
'driver' => $options['conn']['driv'],
'user' => $options['conn']['user'],
'password' => $options['conn']['pass'],
'dbname' => $options['conn']['dbname'],
'host' => $options['conn']['host'],
'charset' => 'utf8',
'driverOptions' => array(
1002 => 'SET NAMES utf8'
)
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
I'm using the following custom bootstrap resource to initialize doctrine therefore $options is in application.ini and is accessible there by $this->getOptions();
// \library\My\Application\Resource\Doctrine.php
class My_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$options = $this->getOptions();
$config = new \Doctrine\ORM\Configuration();
//doctrine autoloader, config and other initializations
...
$connectionOptions = array(
.... //see above
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$registry = Zend_Registry::getInstance();
$registry->em = $em;
return $em;
}
}
It will bootstrap automatically if you put in application.ini
resources.doctrine.conn.host = '127.0.0.1'
resources.doctrine.conn.user = '...'
resources.doctrine.conn.pass = '...'
....
works fine for me
resources.doctrine.dbal.connections.default.parameters.driverOptions.1002 = "SET NAMES 'UTF8'"
1002 is the integer value of PDO::MYSQL_ATTR_INIT_COMMAND:
Command to execute when connecting to the MySQL server. Will
automatically be re-executed when reconnecting.
Note, this constant can only be used in the driver_options array when constructing a new
database handle.
this worked for me. config/autoload/doctrine.local.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => '...',
'password' => '...',
'dbname' => '...',
'driverOptions' => array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
)
),
)
)
)
);
It is possible to add it via application.ini, provided you use ZendX_Doctrine2 (at https://github.com/mridgway/ZendX_Doctrine2) with MySQL.
Then here's the line you need in application.ini:
resources.entitymanagerfactory.connectionOptions.driverOptions.1002 = "SET NAMES utf8"
(1002 == PDO::MYSQL_ATTR_INIT_COMMAND)
Don't forget to correctly set
default-character-set=utf8
in your my.cnf
Since this is for Doctrine 2, and ZendCasts is using Bisna, I believe you can just add this to your configuration.ini file
resources.doctrine.dbal.connections.default.parameters.driverOptions.charset = "utf8"
I'm not exactly sure how to test if it is sticking or not but let us know.
You could set the default table charset like that to utf8:
// Create new Doctrine Manager instance
$doctrineManager = Doctrine_Manager::getInstance();
// Set charset to UTF8
$doctrineManager->setAttribute(
Doctrine_Core::ATTR_DEFAULT_TABLE_CHARSET,
'utf8'
);
Quote:
an _initDoctrine method in the Bootstratp file ?
Yes.
For LoSo library and Doctrine 2 and MySQL add
resources.doctrine2.connection.driverOptions.1002 = "SET NAMES 'UTF8'"
to your application.ini
I have this in my bootstrap:
protected function _initDoctrineLibrary()
{
require_once('Doctrine/Doctrine.php');
$this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine', 'autoload'),'Doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
$config = $this->getOption('doctrine');
$conn = Doctrine_Manager::connection($config['dsn'],'doctrine');
$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
return $conn;
}
where in the application.ini you see
doctrine.dsn = "mysql://user:password#host/databasename"
I think you can do something similar