ZFでお聞きしていることはView Helperであると思います。ビューヘルパーはモデルからデータを取り出し、コントローラで処理することなくビューにダンプできます。ここで簡単な例である:
<?php
class Zend_View_Helper_Track extends Zend_View_Helper_Abstract
{
/**
*
* @param type $trackId
* @return type object
*/
public function Track($trackId) {
//this model just aggregates data from several DbTable models
$track = new Application_Model_TrackInfo();
$data = $track->getByTrackId($trackId);
return $data;
}
}
ビューヘルパーは、いくつかのデータ(オブジェクト、文字列、アレイ、ブール値)を返すことによって特徴付けられるとビューとパーシャルにデータを供給するために使用することができます。
次は、いくつかのビューヘルパーを使用してデータを表示する、partialの例です。限りDBTABLEモデルはフロントエンドのために無用であるとして、あなたは驚くかもしれません
<?php echo $this->partial('_dates.phtml', $this->memberData) ?>
:
<fieldset><legend>Dates and Qualifications</legend>
<table>
<tr>
<td>Birth Date: </td><td><?php echo $this->escape($this->FormatDate($this->bdate)) ?></td>
</tr>
<tr>
<td>Seniority Date: </td><td><?php echo $this->escape($this->FormatDate($this->sendate)) ?></td>
</tr>
</table>
<table>
<tr>
<td>I'm a Lead:</td><td><?php echo $this->escape(ucfirst($this->ToBool($this->lead))) ?></td>
</tr>
<tr>
<td>Lead Date:</td><td><?php echo $this->escape($this->FormatDate($this->ldate)) ?></td>
</tr>
<tr>
<td>I'm an Inspector:</td><td><?php echo $this->escape(ucfirst($this->toBool($this->inspector))) ?></td>
</tr>
<tr>
<td>Admin Login:</td><td><?php echo $this->escape(ucfirst($this->toBool($this->admin))) ?></td>
</tr>
</table>
</fieldset>
とは最終的に私はビューでこの部分を呼び出します。 DbTableクラスにテーブル間にrelationshipsが正しく設定されると、できることの機能がうまく機能します。しかし、ほとんどの人に似ている場合は、DbTableクラスとアプリケーションの間に少なくとも1つのドメインモデル(mappers、service、repository)が存在する可能性があります。
これはリレーションシップを持つモデルです。ナビゲーションを構築するためにデータを供給することが唯一の目的です。
<?php
class Application_Model_DbTable_Menu extends Zend_Db_Table_Abstract {
protected $_name = 'menus';
protected $_dependentTables = array('Application_Model_DbTable_MenuItem');
protected $_referenceMap = array(
'Menu' => array(
'columns' => array('parent_id'),
'refTableClass' => 'Application_Model_DbTable_Menu',
'refColumns' => array('id'),
'onDelete' => self::CASCADE,
'onUpdate' => self::RESTRICT
)
);
public function createMenu($name) {
$row = $this->createRow();
$row->name = $name;
return $row->save();
}
public function getMenus() {
$select = $this->select();
$select->order('name');
$menus = $this->fetchAll($select);
if ($menus->count() > 0) {
return $menus;
} else {
return NULL;
}
}
public function updateMenu($id, $name) {
$currentMenu = $this->find($id)->current();
if ($currentMenu) {
//clear the cache entry for this menu
$cache = Zend_Registry::get('cache');
$id = 'menu_' . $id;
$cache->remove($id);
$currentMenu->name = $name;
return $currentMenu->save();
} else {
return FALSE;
}
}
public function deleteMenu($menuId) {
$row = $this->find($menuId)->current();
if ($row) {
return $row->delete();
} else {
throw new Zend_Exception("Error loading menu...");
}
}
}
Zend_Db_Table_Abstractはそのいくつかのデータ・アクセス・パターンのためのインタフェースを提供し、あなただけのビジネスロジックを提供する必要があり、あなたが欲しい抽象化のレベルを何でも。
私はフロントエンドのデータ操作と表示に同じDBTableクラス(バックエンドで使用)を使用していますか? – ktm
@Rajendra私たちは私たちが循環的な議論に参加していると思います。コントローラとビューにデータを提供するdbTableを使って編集を投稿しました。 – RockyFord