- 2008-04-10 (Thu) 7:00
- Zend_Db
application/config.ini with the following content.Please modify
database.params.* properly.
[staging] database.adapter = pdo_mysql database.params.host = localhost database.params.username = db_user database.params.password = db_password database.params.dbname = db_name
In this article, we will use the following sql.
CREATE TABLE profiles ( id integer AUTO_INCREMENT NOT NULL PRIMARY KEY, user_id integer NOT NULL, first_name varchar(100) NOT NULL, last_name varchar(100) NOT NULL, email varchar(200) NOT NULL, url varchar(200) NOT NULL ); INSERT INTO profiles (user_id, first_name, last_name, email, url) VALUES (1, 'Myfirstname', 'Mylastname', 'my@emailaddress', 'http://myurl/my/page');
application/constorllers/IndexController.php with the following content.
<?php require_once 'Zend/Controller/Action.php'; require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; class IndexController extends Zend_Controller_Action { protected function getDbAdapter() { $config = new Zend_Config_Ini(CONFIG_PATH, 'staging'); $dbAdapter = Zend_Db::factory($config->database); return $dbAdapter; } public function indexAction() { $db = $this->getDbAdapter(); $sql =<<<EOSQL <|!REG3XP0!>SELECT id ,user_id ,first_name ,last_name ,email ,url FROM profiles WHERE user_id = :user_id|> EOSQL; $params = array('user_id' => 1); $db->setFetchMode(Zend_Db::FETCH_ASSOC); $row = $db->fetchRow($sql, $params); if ($row) { $this->view->profile = $row; } } }
application/views/scripts/index/index.phtml with the following content.
<h1><?= $this->translate('Your Profile') ?></h1> <?php if (!$this->profile) : ?> <span><?= $this->translate('No Profile Found') ?></span> <?php else : ?> <dl> <?php foreach($this->profile as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?>
Comments:2
- legal101205 08-04-17 (Thu) 10:38
-
Is there a way to get the dbadapter object in the IndexController.php from the bootstrap.php file ?
For example, with:
$config = new Zend_Config_Ini(CONFIG_PATH, ’staging’);
$dbAdapter = Zend_Db::factory($config->database);
in the bootstrap file, how can i do ?Hope i am clear!
Thx for your great job, it’s a amazing that there is no comment in your articles !Nicolas
- oplabo 08-04-17 (Thu) 15:07
-
Thank you for your comment, Nicolas.
How about using Zend_Registry?
For example :
$dbAdapter = Zend_Db::factory($config->database);
Zend_Registry::set(’my_db’, $dbAdapter);
in the bootstrap file, and
$dbAdapter = Zend_Registry::get(’my_db’);
in the controller.Detailed information is on
Programmer’s Reference Guide(10.5.3)
> Thx for your great job, it’s a amazing that there is no comment in your articles !
This site just started, and I also started the study of Zend Framework recently.
So I wish to get more skill together!
Trackbacks:0
- Trackback URL for this entry
- http://www.oplabo.com/article/17/trackback
- Listed below are links to weblogs that reference
- DB Connection using Zend_Db from Open Programming Laboratory