0

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.

1
  • I got the answer at my own.We have to use like that: $table = new Admin_Model_DbTable_DeliveryCenter(); $select = $table->select(Zend_Db_Table::SELECT_WITH_FROM_PART); $select->setIntegrityCheck(false) ->join('region', 'region.region_id = delivery_center.region_id'); $result = $table->fetchAll($select); Commented Dec 21, 2011 at 14:57

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.