Urlsを使ったものを紹介します。
Flowなどでも同じように出来ます。
と言ってもページャー、ソート、表示項目の選択など様々なことが出来ます。
- Viewsをextendしたクラスを使う
コード量は若干増えますが後で拡張しやすくなります。
index.php
<?php
require_once("./__init__.php");
Rhaco::import("generic.Urls");
Rhaco::import("CRUD");
$pattern = array(
"^list\/?$"=>array("class"=>"CRUD","method"=>"productList"),
"^detail\/([0-9]+?)\/?$"=>array("class"=>"CRUD","method"=>"productDetail"),
"^create\/?$"=>array("class"=>"CRUD","method"=>"productCreate"),
"^update\/([0-9]+?)\/?$"=>array("class"=>"CRUD","method"=>"productUpdate"),
"^delete\/([0-9]+?)\/?$"=>array("class"=>"CRUD","method"=>"productDelete")
);
$parser = Urls::parser($pattern);
$parser->write();
?>
library/CRUD.php
<?php
Rhaco::import("generic.Views");
Rhaco::import("model.Product");
class CRUD extends Views {
function productList(){
return $this->read(new Product(), new C(Q::like(Product::name(),"hoge","p")));
}
function productDetail($id){
return $this->detail(new Product($id));
}
function productCreate(){
return $this->create('path/to/redirect',$this->toObject(new Product()));
}
function productUpdate($id){
return $this->update('path/to/redirect',$this->toObject(new Product()),new C(Q::eq(Product::columnId(),$id)));
}
function productDelete($id){
return $this->drop('path/to/redirect',new Product($id));
}
}
?>
- 独自クラスを使う
柔軟性が高く、この中でViewsを使うことも当然出来るので便利。
index.php
<?php
require_once("./__init__.php");
Rhaco::import("generic.Urls");
Rhaco::import("CRUD");
$pattern = array(
"^list\/?$"=>array("class"=>"CRUD","method"=>"read"),
"^detail\/([0-9]+?)\/?$"=>array("class"=>"CRUD","method"=>"detail"),
"^create\/?$"=>array("class"=>"CRUD","method"=>"create"),
"^update\/([0-9]+?)\/?$"=>array("class"=>"CRUD","method"=>"update"),
"^delete\/([0-9]+?)\/?$"=>array("class"=>"CRUD","method"=>"drop")
);
$parser = Urls::parser($pattern);
$parser->write();
?>
library/CRUD.php
<?php
Rhaco::import("generic.Views");
Rhaco::import("model.Product");
class CRUD {
var $_view;
function CRUD(){
$this->_view = new Views();
}
function read(){
return $this->_view->read(new Product(), new C(Q::like(Product::name(),"hoge","p")));
}
function detail($id){
return $this->_view->detail(new Product($id));
}
function create(){
return $this->_view->create('path/to/redirect',$this->_view->toObject(new Product()));
}
function update($id){
return $this->_view->update('path/to/redirect',$this->_view->toObject(new Product()),new C(Q::eq(Product::columnId(),$id)));
}
function productDelete($id){
return $this->_view->drop('path/to/redirect',new Product($id));
}
}
?>
※注意点
ViewsやViewsMapperを使用し、リスト読み込み(readメソッド)する場合は必ず
Q::fact()が適用されるので、リファレンス先データが無いデータは表示されませんし、少々遅くなります。
速度を気にされる方やリファレンス先のデータが無い場合のある処理の場合はreadを使わないで独自実装した方が良いです。