intro
Zend_Db_Tableを利用してDAOを作成します。これによりDBへの操作を抽象化することができ、可読性・保守性の高いコードになります。ソースコードはこちらからダウンロードできます。
1.bootstrap.phpとconfig.iniの作成
以下のように
以下のように
環境に合わせて
この記事では以下のSQLを使用しました(MySQL)。
application/bootstrap.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php set_include_path('../library' . PATH_SEPARATOR . get_include_path()); define('APP_BASE', '../application'); define('CONFIG_PATH', APP_BASE . '/config.ini'); require_once 'Zend/Controller/Front.php'; require_once 'Zend/Layout.php'; require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db/Table/Abstract.php'; $layout = Zend_Layout::startMvc(); //$layout->getView()->baseUrl = '/yourBaseUrl'; $config = new Zend_Config_Ini(CONFIG_PATH, 'staging'); $params = $config->database->params->toArray(); $params['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER; $dbAdapter = Zend_Db::factory($config->database->adapter, $params); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); Zend_Controller_Front::run(APP_BASE . '/controllers'); |
以下のように
application/config.ini
ファイルを作成します。環境に合わせて
database.params
以下を変更してください。
1 2 3 4 5 6 |
[staging] database.adapter = pdo_mysql database.params.host = localhost database.params.username = db_user database.params.password = db_password database.params.dbname = db_name |
この記事では以下のSQLを使用しました(MySQL)。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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'); |
2.Zend_Db_TableによるDAOの作成
以下のように
application/models/tables/Profiles.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php require_once 'Zend/Db/Table/Abstract.php'; class Profiles extends Zend_Db_Table_Abstract { protected $_name = 'profiles'; public function findByUserId($user_id) { $select = $this->select()->where('user_id = ?', $user_id); return $this->fetchAll($select); } } |
3.コントローラの作成
以下のように
application/constorllers/IndexController.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php require_once 'Zend/Controller/Action.php'; require_once APP_BASE . '/models/Profiles.php'; class IndexController extends Zend_Controller_Action { public function indexAction() { $profiles = new Profiles(); $row =$profiles->findByUserId(1)->current(); if ($row) { $this->view->profile = $row->toArray(); } } } |
4.ビューの作成
以下のように
application/views/scripts/index/index.phtml
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 |
<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; ?> |
5.確認
Webサーバにアクセスしてデータベースのデータが表示されることを確認してみてください。
参考
find系メソッドのマジックメソッドによる実装については以下に関連情報があります。