良い一日。 Zend Frameworkは、プロジェクトの開発時間を大幅に短縮できる優れたツールであることに同意するでしょう(ただし、それだけでなく) 。 そのような場所の1つは、データベーステーブルモデルです。
Zend_Db_Tableでは、CRUD操作を簡単に実行できます。
それで、たとえばCMSのモジュールを書くとき、私たちは常にどんなアクションを実行しますか?
- アイテムを追加
- 任意のフィールドでアイテムを選択します
- アイテムを削除
- アイテムを更新
それでは始めましょう。
クラスライブラリ/ App / Db / Table.phpを作成します
<?php
class App_Db_Table extends Zend_Db_Table_Abstract
{
}
* This source code was highlighted with Source Code Highlighter .
アイテムを追加するメソッドを作成します
public function addItem($data){
if (empty($data)) {
throw new Exception( " " );
}
return $ this ->insert($data);
}
* This source code was highlighted with Source Code Highlighter .
次に、idキーフィールドで要素を更新するメソッドを作成しましょう
public function updateItemById($id, $data = NULL){
if (empty($id)) {
throw new Exception( " id " );
}
if (empty($data)) {
throw new Exception( " " );
}
$ where = $ this ->getAdapter()->quoteInto( 'id = ?' , ( int )$id);
return $ this ->update($data, $ where );
}
* This source code was highlighted with Source Code Highlighter .
IDキーフィールドで要素を削除する方法
public function deleteItemById($id){
if (empty($id)) {
throw new Exception( " id " );
}
$result = $ this ->delete(array( 'id = ?' => ( int )$id));
if (0 === $result) {
throw new Exception( " " );
}
}
* This source code was highlighted with Source Code Highlighter .
IDキーフィールドで要素を選択する方法
public function getItemById($id){
if (empty($id)) {
throw new Exception( " id " );
}
$ select = $ this ->getAdapter()->quoteInto( 'id = ?' , ( int )$id);
$result = $ this ->fetchRow($ select );
if (NULL !== $result) {
return $result->toArray();
} else {
throw new Exception( " " );
}
}
* This source code was highlighted with Source Code Highlighter .
また、便宜上、マジックメソッドgetItemsByを使用します。これにより、任意のフィールドの値で要素を選択できます。
public function getItemsBy($key, $ value ){
$ where = $ this ->getAdapter()->quoteInto( "$key = ?" , $ value );
$result = $ this ->fetchAll($ where )->toArray();
if (count($result) > 0) {
return $result;
} else {
throw new Exception( " " );
}
}
public function __call($name, $arguments) {
if (0 === strpos($name, 'getItemsBy' )) {
array_unshift($arguments, substr($name, 10));
return call_user_func_array(array($ this , 'getItemsBy' ), $arguments);
}
throw new Exception( " " .$name);
}
* This source code was highlighted with Source Code Highlighter .
今それを使用する方法? テーブルモデルテスト(applicaiton / models / DbTable / Test.php)を作成して、作成したクラスを展開します。ここで、作成したすべてのメソッドが利用可能になります。
class Model_DbTable_Test extends App_Db_Table
{
protected $_name = 'test' ;
}
* This source code was highlighted with Source Code Highlighter .
コントローラーの例
// Test
$model = new Model_DbTable_Test();
// id = 1
try {
$item = $model->getItemById(1);
}
catch (Exception $e){
print $e->getMessage();
}
// getItemsBy()
try {
$item = $model->getItemsByName( 'itemname' );
}
catch (Exception $e){
print $e->getMessage();
}
* This source code was highlighted with Source Code Highlighter .
これらは、頻繁に使用するために個別に設定できるほんの数例のメソッドです。 また、アイテムを更新および削除したり、ディレクトリブランチを管理したりするためのマジックメソッドを記述することもできます。
PSは、.ini構成などで、クラスを自動ロードするためのライブラリ/アプリを追加することを忘れないでください
autoloaderNamespaces [] = "App_"