トップ 差分 一覧 ソース 検索 ヘルプ PDF RSS ログイン

Zend_Db

[カテゴリ:言語]
[カテゴリ:PHP]
[カテゴリ:PHP5]

Zend_Db (2008/05/11)

参考

PHP: PDO - Manual
# 公式マニュアル PDO
http://jp2.php.net/manual/ja/book.pdo.php

Zend Framework: Documentation
# 公式マニュアル Zend_Db
http://framework.zend.com/manual/ja/zend.db.html

{ Online Notes } » PHP Development
# en)Zend_Db_Table_Abstract の使い方まとめ
http://www.armando.ws/?cat=6

wads-blog » Zend Framework
# いろいろなクラスのTips
http://wadslab.net/category/zend-framework/

サンプルスクリプト

ソース

<?php

$include_add = array();
$include_add[] = 'C:/xampp/php/ZendFramework-1.5.1/library/';
foreach ($include_add as $dir) {
    set_include_path($dir . PATH_SEPARATOR . get_include_path());
}

require_once 'Zend/Debug.php';
require_once 'Zend/Db.php';

/* @var $db Zend_Db_Adapter_Abstract */
$db = null;

/**
 * 初期化
 */
function init()
{
    global $db;
    $ret = array();
    $params = array(
        'host'     => '127.0.0.1',
        'username' => 'root',
        'password' => '',
        'dbname'   => 'test',
    );
    $db = Zend_Db::factory('Pdo_Mysql', $params);
    $ret[] = get_class($db);
    $ret[] = get_class($db->getConnection());   // 遅延接続するときは不要
    return $ret;
}

/**
 * TABLE作成
 */
function create_table()
{
    global $db;
    $ret = array();
    $ret[] = get_class($db->query('DROP TABLE IF EXISTS bugs'));
$ddl = <<< EOD
CREATE TABLE bugs (
  bug_id            INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
  bug_description   VARCHAR(100),
  bug_status        VARCHAR(20)
);
EOD;
    $ret[] = get_class($db->query($ddl));
    return $ret;

//  $pdo_obj = $db->getConnection();
//  $ret[] = get_class($pdo_obj);
//  $ret[] = get_class($pdo_obj->exec('DROP TABLE IF EXISTS bugs'));
//$ddl = <<< EOD
//CREATE TABLE bugs (
//  bug_id            INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
//  bug_description   VARCHAR(100),
//  bug_status        VARCHAR(20)
//);
//EOD;
//  $ret[] = get_class($pdo_obj->exec($ddl));
//  return $ret;
}

/**
 * 行挿入
 */
function insert()
{
    global $db;
    $ret = array();
    $ret[] = $db->insert(
        'bugs',
        array(
            'bug_description' => '11',
            'bug_status'      => '111',
        )
    );
    $ret[] = $db->lastInsertId();
    $ret[] = $db->insert(
        'bugs',
        array(
            'bug_description' => '22',
            'bug_status'      => '222',
        )
    );
    $ret[] = $db->lastInsertId();
    return $ret;
}

/**
 * SELECT直接実行
 */
function query()
{
    global $db;
    $ret = array();
    $stmt = $db->query('SELECT * FROM bugs');
    $ret[] = get_class($stmt);
    $rows = array();
    while ($row = $stmt->fetch()) {
        $rows[] = $row;
    }
    $ret[] = $rows;
    return $ret;
}

/**
 * SELECT直接実行、結果をオブジェクトで取得
 */
function query_fechobject()
{
    global $db;
    $ret = array();
    $stmt = $db->query('SELECT * FROM bugs');
    $ret[] = get_class($stmt);
    $rows = array();
    while ($row = $stmt->fetchObject()) {
        $rows[] = $row;
    }
    $ret[] = $rows;
    return $ret;
}

/**
 * Zend_SelectでSELECT実行
 */
function select_1()
{
    global $db;
    $ret = array();
    $select = $db->select()
        ->from('bugs')
        ->where('bug_id = 1');
    $ret[] = $select->__toString();
    $stmt = $select->query();
    $ret[] = get_class($stmt);
    while ($row = $stmt->fetch()) {
        $rows[] = $row;
    }
    $ret[] = $rows;
    return $ret;
}

/**
 * Zend_SelectでSELECT実行
 */
function select_2()
{
    global $db;
    $ret = array();
    $select = $db->select()
        ->from(array('b' => 'bugs'), array('bug_id', 'bug_description'))
        ->where('bug_id = 1');
    $ret[] = $select->__toString();
    $stmt = $select->query();
    $ret[] = get_class($stmt);
    while ($row = $stmt->fetchObject()) {
        $rows[] = $row;
    }
    $ret[] = $rows;
    return $ret;
}

Zend_Debug::dump(init(),                'init()');
Zend_Debug::dump(create_table(),        'create_table()');
Zend_Debug::dump(insert(),              'insert()');
Zend_Debug::dump(query(),               'query()');
Zend_Debug::dump(query_fechobject(),    'query_fechobject()');
Zend_Debug::dump(select_1(),            'select_1()');
Zend_Debug::dump(select_2(),            'select_2()');

?>

実行結果

init() array(2) {
  [0] => string(25) "Zend_Db_Adapter_Pdo_Mysql"
  [1] => string(3) "PDO"
}

create_table() array(2) {
  [0] => string(21) "Zend_Db_Statement_Pdo"
  [1] => string(21) "Zend_Db_Statement_Pdo"
}

insert() array(4) {
  [0] => int(1)
  [1] => string(1) "1"
  [2] => int(1)
  [3] => string(1) "2"
}

query() array(2) {
  [0] => string(21) "Zend_Db_Statement_Pdo"
  [1] => array(2) {
    [0] => array(3) {
      ["bug_id"] => string(1) "1"
      ["bug_description"] => string(2) "11"
      ["bug_status"] => string(3) "111"
    }
    [1] => array(3) {
      ["bug_id"] => string(1) "2"
      ["bug_description"] => string(2) "22"
      ["bug_status"] => string(3) "222"
    }
  }
}

query_fechobject() array(2) {
  [0] => string(21) "Zend_Db_Statement_Pdo"
  [1] => array(2) {
    [0] => object(stdClass)#6 (3) {
      ["bug_id"] => string(1) "1"
      ["bug_description"] => string(2) "11"
      ["bug_status"] => string(3) "111"
    }
    [1] => object(stdClass)#7 (3) {
      ["bug_id"] => string(1) "2"
      ["bug_description"] => string(2) "22"
      ["bug_status"] => string(3) "222"
    }
  }
}

select_1() array(3) {
  [0] => string(46) "SELECT `bugs`.* FROM `bugs` WHERE (bug_id = 1)"
  [1] => string(21) "Zend_Db_Statement_Pdo"
  [2] => array(1) {
    [0] => array(3) {
      ["bug_id"] => string(1) "1"
      ["bug_description"] => string(2) "11"
      ["bug_status"] => string(3) "111"
    }
  }
}

select_2() array(3) {
  [0] => string(80) "SELECT `b`.`bug_id`, `b`.`bug_description` FROM `bugs` AS `b` WHERE (bug_id = 1)"
  [1] => string(21) "Zend_Db_Statement_Pdo"
  [2] => array(1) {
    [0] => object(stdClass)#5 (2) {
      ["bug_id"] => string(1) "1"
      ["bug_description"] => string(2) "11"
    }
  }
}

Zend_Db_Adapter_Mysqli を使ったときのDLL実行方法 (2009/11/18)

Zend_Db上でDDLは実装されていないので、接続方法に応じてネイティブの実行方法を通す必要がある。
Zend_Db_Adapter_Mysqliの場合、getConnection()で素のMysqliクラス(オブジェクト)を得て実行する。

$db->getConnection()->query("truncate table hogehoge");

最終更新時間:2009年11月18日 16時02分37秒