Zend FrameworkでStructure_DataGridを使うための独自データソース習作

PearのStructures_DataGridは激しく便利ですね。本来はDB_DataObject や MDB2などをデータソース(ドライバ)として接続するのですが、Zend_Db_Tableでデータベースに接続できるのに、わざわざ新しい接続先を作るのもどうかなあと思いますので、Zend_Db_Tableを用いたデータソースのクラス「Common_Db_Structures_Datasource」を作成してみることにします。

[php]
require 'Structures/DataGrid/DataSource.php';

class Common_Db_Structures_Datasource extends Structures_DataGrid_DataSource
{
/**
* Zend_Db_Table_Abstractの派生クラスを持ちます。
*
* @var Zend_Db_Table_Abstract $this->_table
*/
private $_table;

/**
* テーブルの情報を持ちます。
*
* @var Array $this->_info
*/
private $_info;

/**
* Zend_Db_Select クラスを持ちます。
*
* @var Zend_Db_Select $this->_select
*/
private $_select;

/**
* コンストラクタです。
*
* @param Zend_Db_Table_Abstract $table
*/
public function __construct(Zend_Db_Table_Abstract $table) {
$this->_table = $table;
$this->_info = $table->info();
$this->_select = $table->getAdapter()->select()->from($this->_info['name']);
parent::Structures_DataGrid_DataSource();
}

/**
* レコードの総数を返します。必須メソッドです。
*
* @return int $count レコードの総数
*/
public function count() {
$db = $this->_table->getAdapter();
try {
$count = $db->fetchOne(“SELECT COUNT(*) FROM ” . $this->_info['name']);
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}

return (int) $count;
}

/**
* データの並べ替えを行います。必須メソッドです。
*/
public function sort($sortSpec, $sortDir = ‘ASC’) {
$this->_select->order($sortSpec . ‘ ‘ . $sortDir);
}

/**
* 表示するデータを返します。必須メソッドです。
*
* @param int $offset offsetで指定した場所のレコードからデータを取得します。
* @param int $len lenで指定した件数のデータを取得します。
* @return Array 二次元配列のデータです。
*/
public function fetch($offset = 0, $len = null)
{
// $len = NULLのママだと、offsetが効かない。MySQLのみ?
// MySQLのリファレンスで指定されている数値が大きすぎてPHPでfloatになってしまう無念。要改善。
$len = is_null($len) ? $this->count() : $len;
# $len = is_null($len) ? 18446744073709551615 : $len;

$this->_select->limit($len, $offset);
try {
$stmt = $this->_select->query();
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}

return $stmt->fetchAll();
}
}

?>

[/php]

上記のデータソースを使って、Structure_DataGridを使うには、以下のようにコーディングします。

[php]
$table = new TestTables(); // Zend_Db_Table_Abstractの派生クラス。
$datagrid = new Structures_DataGrid(20); // 最大表示20件。
$datagrid->setDefaultSort(array(‘register_date’ => “DESC”)); // デフォルトのソート形式を指定。bindする前にセットしなければならない。
$datagrid->bindDataSource(new Common_Db_Structures_Datasource($table)); // Zend_Db_Table_Abstractの派生クラスを上記クラスでデータソースとする。
// 素のママだとリンク先がZend Frameworkの起動ファイルになってしまうので、変更。
$options = array(
‘selfPath’ => $this->getRequest()->getPathInfo(),
);
$datagrid->render(null, $options);
[/php]

Common_Db_Structures_Datasource
を作成しておくことで、Zend_Db_Tableの設定をそのままStructures_Gridに使うことが出来るようになります。

関連する記事:

Powered by

  1. 上記ソースに一時、
    $this->_select->from(“hoge”);
    というゴミが入ってました。すいません。

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">