Zend Adapter の要点を理解しようとしています。チュートリアルと説明は紛らわしいので、もっと明確にする必要があります。SQLクエリの操作方法と目的のSQL結果を取得する方法を完全に理解するための簡単な例を教えてもらえますか?
特に、私は取得する方法を学ぶことに興味があります
-- 列名
-- テーブル名
-- fetchAll エントリ
や。。など。
ありがとうございました、
Zend Adapter の要点を理解しようとしています。チュートリアルと説明は紛らわしいので、もっと明確にする必要があります。SQLクエリの操作方法と目的のSQL結果を取得する方法を完全に理解するための簡単な例を教えてもらえますか?
特に、私は取得する方法を学ぶことに興味があります
-- 列名
-- テーブル名
-- fetchAll エントリ
や。。など。
ありがとうございました、
他の多くの人と同じように、クイックスタートで問題を抱えているようです。Rob Allenのチュートリアルを試してみてください。
テーブルへの接続方法には複数の選択肢があり、Zend_Db との混乱はここから始まることがよくあります。
1 つの DB を使用する場合の最も簡単な方法は、application.ini
ファイルに少なくとも次の行を追加することです。
resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user_name"
resources.db.params.password = "password"
resources.db.params.dbname = "db_name"
別の方法として、 Zend_Db_Adapterを使用して、コードのほぼどこからでもデータベースに接続できます。
//using a normal constructor
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
//using factory
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
アプリケーションでこれを使用すると、次のように簡単になります。
//fetchAll using Zend_Db_Adapter and plain SQL
$sql = 'SELECT * FROM bugs WHERE bug_id = ?';
$result = $db->fetchAll($sql, 2);
データベース内のテーブルを一覧表示できます。
$tables = $db->listTables();
または、完全なテーブルの説明 (列名を含む) を取得できます。Zend_Db_Adapter_Abstract の関数からコメント ブロックを含めました。
/**
* Returns the column descriptions for a table.
*
* The return value is an associative array keyed by the column name,
* as returned by the RDBMS.
*
* The value of each array element is an associative array
* with the following keys:
*
* SCHEMA_NAME => string; name of database or schema
* TABLE_NAME => string;
* COLUMN_NAME => string; column name
* COLUMN_POSITION => number; ordinal position of column in table
* DATA_TYPE => string; SQL datatype name of column
* DEFAULT => string; default expression of column, null if none
* NULLABLE => boolean; true if column can have nulls
* LENGTH => number; length of CHAR/VARCHAR
* SCALE => number; scale of NUMERIC/DECIMAL
* PRECISION => number; precision of NUMERIC/DECIMAL
* UNSIGNED => boolean; unsigned property of an integer type
* PRIMARY => boolean; true if column is part of the primary key
* PRIMARY_POSITION => integer; position of column in primary key
*
* @param string $tableName
* @param string $schemaName OPTIONAL
* @return array
*/
$describTable = $db->describeTable('myTable');
この情報を参考にしてください。しかし、Zend_Db の本当の力の多くはZend_Db_Table、Zend_Db_Table_Row、特にZend_Db_Selectクラスにあることがわかりました。
時間をかけてそれらを理解することをお勧めします。
Zend_Db_Table と Zend_Db_select から期待できることの例として (より高度なマッパーとドメイン オブジェクトを使用していない場合、うまくいけば後でそれらが登場します):
//When using DbTable models that extend Zend_Db_Table_Abstract the model already
//knows the name of the table and has full access to the Db adapter, allowing your code to
//be very brief and descriptive.
class Application_Model_DbTable_Weekend extends Zend_Db_Table_Abstract
{
//name of table, required if classname is not the same as the table name
protected $_name = 'weekend';
//primary key column of table, a good idea especially if primary key is not 'id'
protected $_primary = 'weekendid';
public function getWeekend($weekendId) {
//create select object
$select = $this->select();
$select->where('weekendid = ?', $weekendId);//placeholder syntax
$result = $this->fetchRow($select);
if (!$result) {
throw new Exception('Could not find weekend ID ' . $weekendId);
}
return $result;//returns a single row object
}
public function fetchAllWeekend() {
$select = $this->select();
$result = $this->fetchAll($select);
return $result; //returns array of row objects (rowset object)
}
}
Rob Allen の Zf チュートリアルでは、DbTable モデルがどのようにセットアップされ、どのように機能するかについて説明します。
お役に立てれば...