Use of select queries in Zend Framework without providing database info - zend-framework

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".

Related

Zend Framework 1.12 cant connect to database with Db_Table

I am creating my second Zend Framework project (using 1.12) and have set it up just like the first (uses 1.11), but for some reason I am having problems connecting to my database using my class extending Zend_Db_Table. I have my database configuration in an ini file like this:
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "myUsername"
resources.db.params.password = "myPassword"
resources.db.params.dbname = "myDbname"
resources.db.isDefaultTableAdapter = true
Then I have my table class:
class Application_Model_DbTable_TestData extends Zend_Db_Table_Abstract {
protected $_name = 'testdata'
}
and then in my controller I try:
$testdata = new Application_Model_DbTable_TestData();
and then when I go to the view for that controller I just get the message Application Error with no other info.
If I do the following in the controller:
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => 'localhost',
'username' => 'myUsername',
'password' => 'myPassword',
'dbname' => 'myDbname'
));
$test = $db->select()->from(array('test' => 'testdata'));
I can get the data. Any ideas why I can't do it with the table class? As far as I can tell it's exactly the same as my other set up which works just fine.
It turned out that the problem was where I had the database configuration data placed in my ini file. It was directly under [development : production] so development was the only thing that was getting it. Just one of those stupid oversights where I had to step away for a while and look again later to see it.

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.

zend database connection

In one of our project the database connection specified in application.ini is
resources.db.adapter = Mysqli
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = zf_tutorial
But I required to call the host, username, password and dbname from another file. how to do this?
usually with your database setup in the application.ini calling the default adapter is trivial
$db = Zend_Db_Table::getDefaultAdapter();
adding resources.db.isDefaultTableAdapter = true to the application.ini makes sure you're calling the correct adapter.
There are very situations where this may not work properly and most of them involve bootstrapping.
Usually, connection to the DB sets up in index.php:
// TODO: $adapter and $params from custom place, not hardcoded
$adapter = 'pdo_mysql';
$params = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'mydb'
);
$db = Zend_Db::factory($adapter, $params);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
In this code $adapter and $params are hardcoded. You can get $adapter and $params from your custom file.
The accepted answer here is not a good way to get the db object, it should be set up in application.ini as you were doing originally.
If you want to get another instance of the db object you have several options; you can do as #RockyFord suggests, or you can get the db object from any controller like this:-
$db = $this->getInvokeArg('bootstrap')->getResource('db');
If you want to go further and have access to the connection details (I can't imagine why you would need them again) then you can get them from the db object like this:-
$dbConfig = $db->getConfig();
or indeed:-
$dbConfig = $this->getInvokeArg('bootstrap')->getResource('db')->getConfig();
Which will give you an array containing the connection data, you can try this and do a var_dump($dbConfig); to see the details.
This is a much more robust way of accessing the db object if you need to, although I would take a long hard look at your overall code design if you need to do this in more than one place, especially if you are accessing the connection details as described here.

what is the benifit of using Zend_Config class?

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