what is the benifit of using Zend_Config class? - zend-framework

i am new to zend framework i wana know why we are using this code for connection to database although we can use the below code also which is simple and not including class what is the advantages of including Zend_config( )
require_once 'Zend/Config.php';
$arrConfig = array(
'webhost'=>'localhost',
'appName'=>'My First Zend',
'database'=>array(
'dbhost'=>'localhost',
'dbname'=>'zend',
'dbuser'=>'root',
'dbpass'=>'admin'
)
);
$config = new Zend_Config($arrConfig);
$params = array('host' =>$config->database->dbhost,
'username' =>$config->database->dbuser,
'password' =>$config->database->dbpass,
'dbname' =>$config->database->dbname
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
if i can do like this
include_once 'Zend/Db/Adapter/Pdo/Mysql.php';
$params = array('host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'zend'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$DB->setFetchMode(Zend_Db::FETCH_OBJ);

in the way you're using Zend_Config it's in fact doesn't help you a lot to have the settings in a config object.
Usually, in ZF applications, there's a separate application.ini file which contains all the settings:
$config = new Zend_Config_Ini('/path/to/config.ini',
'production');
Then it's convenient to separate environments (production & development for example) into different sections:
; Production site configuration data
[production]
database.adapter = pdo_mysql
database.params.host = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname = dbname
; Development site configuration data inherits from production and
; overrides values as necessary
[development : production]
database.params.host = dev.example.com
database.params.username = devuser
database.params.password = devsecret
meaning loading the config with:
$config = new Zend_Config_Ini('/path/to/config.ini',
'development');
wil return the development config.
http://framework.zend.com/manual/en/zend.config.adapters.ini.html

Related

how to connect zend_Db in one file

when i want to connect database in zend framework with zend_Db, in each controller or model must write this code:
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test',
'charset' => 'utf8'
);
but when use Doctrine it's enough to write this code in application.ini
doctrine.dsn = "mysql://user:pass#localhost/dbase"
how i can use zend_Db with out set connection setting in each file?
open
application/configs/application.ini
add following lines
resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =
resources.db.params.dbname = foo
and then inside your Bootstrap.php
do
public function _initSetup()
{
$this->bootstrap('db');
}
and then whereever or whenever you need db instance simply do
$db = Zend_Db_Table::getDefaultAdapter();
there are two ways one by using your configuration.ini file
by this code :
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = dbname
resources.db.params.driver_options.1002 = "SET NAMES utf8;"
or by using hte adapter class
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
and dont put any function in your bootstrap.php file

Access to database zend framework

I want to access to database "mysql", I read that we can define the db adapter and db configurations by writing these lines in application.ini file
resources.db.adapter = MYSQLI
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =123456
resources.db.params.dbname = visits_db
I want to get the $db object in order to use in for executing sql statements
$db->insert("inspection_visits_tb",
$insepctionVisitData = array(
'day' => $visit->getDay(),
'date' => $visit->getDate(),
'target' => $visit->getTarget()
));
I want to get it from the application.ini file not like this
require_once 'Zend/Db.php';
$db = Zend_Db::factory("MYSQLI",
array(
"host" => "localhost",
"dbname" => "visits_db",
"username" => "root",
"password" => "123456")
);
because I want to define the db adapter in one place only. What should I do to get the $db object??
Set up the db resource in application.ini like you were. Add the isDefaultAdapter option.
resources.db.adapter = "MYSQLI"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "123456"
resources.db.params.dbname = "visits_db"
resources.db.isDefaultAdapter = true
Then to get the $db object...
In Bootstrap.php
protected function _initDb()
{
$this->bootstrap('db');
$db = $this->getResource('db');
Zend_Registry::set('db', $db);
}
From Somewhere else w/out using Registry
$db = Zend_Db_Table::getDefaultAdapter();
Using Zend_Db_Table
$table = new Zend_Db_Table('bugs');
$select = $table->select()...
See also ZF Quickstart, Zend_Db_Table, Zend_Db_Select for more examples.

Use of select queries in Zend Framework without providing database info

I use select queries as by following code:
$params = array(
'username' => 'root',
'password' => '',
'dbname' => 'mst2');
$db = Zend_Db::factory('pdo_mysql', $params);
$select = $db->select()
->from(array('dc' => 'delivery_center'))
->join(array('r' => 'region'), 'dc.region_id = r.region_id');
$stmt = $select->query();
$result = $stmt->fetchAll();
Here $db is the credentials of the database that I am using.But I have specified the credentials in application.ini already by following lines:
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = mst2
Now logically I should not provide these credentials again.But I have to use select queries.So how $db should be initialized without proving database credentials again??Thanks in advance.
Db resource is available by default and is initialized automatically whilst bootstraping.
If you want to get database adapter in your application you can get it as plugin resource from bootstrap:
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
If you do not have reference to bootstrap you always can retrieve it from FrontController:
$front = Zend_Controller_Front::getInstance();
$bootstrap = $front->getParam('bootstrap');
You can use
Zend_Db::factory($zend_config_object->resources->db)
but i think if you have specified this in application.ini, then zend create db object for you automaticlly and you can get it through
Zend_Register and key "db".

Zend Framework IBM DB2 Connection Script application.ini

This is the script I'm trying to convert in to a standard application.ini zend_db connection:
require_once 'Zend/Db/Adapter/Db2.php';
$config = array( 'dbname' => '*LOCAL',
'username' => '',
'password' => '',
'os'=>'i5',
'driver_options'=> array("i5_commit" =>DB2_I5_TXN_READ_UNCOMMITTED,
"autocommit"=>DB2_AUTOCOMMIT_OFF,
"i5_lib"=>'WEBLIB'));
I don't know how to address the driver_options in the application.ini dot syntax. If it was just a regular connection string, I wouldn't have difficulty.
I would appreciate any help or direction.
i use this application.ini config:
resources.db.adapter = "Db2"
resources.db.params.dbname = "*LOCAL"
resources.db.params.ip = "192.168.1.100"
resources.db.params.os = "i5"
resources.db.params.persistent = true
resources.db.params.driver_options.autocommit = DB2_AUTOCOMMIT_ON
resources.db.params.driver_options.i5_naming = DB2_I5_NAMING_ON
resources.db.params.driver_options.i5_libl = "WEBLIB"
resources.db.isDefaultTableAdapter = true

How to define the use of utf-8 in Doctrine 2 in Zend Framework application.ini, when using Bisna

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