我使用zend數據庫表模型進行後端crud操作。不過,我認爲像這樣的模型對於我的前端數據顯示是沒有意義的,例如類別新聞,新聞和博客小部件等等來自各種表格或加入各種表格。zend模型的網站前端
類錯誤擴展Zend_Db_Table_Abstract {
protected $_name = 'bugs'; }
型號這種方式非常適合我的後臺管理面板CRUD操作,我將如何創建前端的操作模式,任何例子是非常appreceated。謝謝
我使用zend數據庫表模型進行後端crud操作。不過,我認爲像這樣的模型對於我的前端數據顯示是沒有意義的,例如類別新聞,新聞和博客小部件等等來自各種表格或加入各種表格。zend模型的網站前端
類錯誤擴展Zend_Db_Table_Abstract {
protected $_name = 'bugs'; }
型號這種方式非常適合我的後臺管理面板CRUD操作,我將如何創建前端的操作模式,任何例子是非常appreceated。謝謝
我認爲你在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的示例。
<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>
,最後我把這部分的視圖:
<?php echo $this->partial('_dates.phtml', $this->memberData) ?>
儘可能DBTABLE模型是無用的前端,你可能會感到驚訝。在DbTable類中正確建立表格之間的relationships之後,他們可以執行的功能就會順利進行。但是,如果你像大多數人一樣,你的DbTable類和你的應用程序之間可能至少有一層域模型(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提供的接口,用於幾個數據訪問模式,你只需要提供你想要的業務邏輯和任何抽象的水平。
即使使用Zend_Db_Table
衍生模型,您也可以加入其他表格。只要確保關閉完整性檢查。
代碼(未測試)可能是這個樣子:
class My_Model_News extends Zend_Db_Table
{
// Hate the 'tbl_' prefix. Just being explicit that this is a
// table name.
protected $_name = 'tbl_news';
public function fetchNewsByAuthor($authorId)
{
$select = $this->select();
$select->setIntegrityCheck(false)
->from(array('n' => 'tbl_news'), array('*'))
->join(array('a' => 'tbl_author'), array('n.author_id = a.id'), array('author_name' => 'a.name'))
->order('n.date_posted DESC');
return $this->fetchAll($select);
}
}
然後在你的控制器:
$authorId = $this->_getParam('authorId');
$newsModel = new My_Model_News();
$this->view->articles = $newsModel->fetchNewsByAuthor($authorId);
說實話,這是的事情之一是讓我扁平的最多TableGateway方法,如Zend_Db_Table
。我發現TableGateway非常適合單表查詢,但我發現我的大多數現實生活情況都需要多表。因此,我最終創建了不綁定到單個表的模型,而是接受一個Zend_Db_Adapter
實例,然後查詢/連接他們需要的任何表。或者,我推出更復雜的ORM,比如教義。
我發現,與定義的關係Db_Table做一個合理的工作,沒有大量的聯接。 Zend_Db也從未打算成爲教義。兩種完全不同的動物,目的和範圍各不相同。 – RockyFord 2012-02-20 05:09:32
@RockyFord:對。 'Zend_Db'並不是一個完整的ORM。它的功能很好:TableGateway。 ;-)我承認我從未完全調查過關係功能。我得到的印象是,通過關係獲取依賴行集會生成一個單獨的查詢。因此,抓取並渲染10篇文章(例如作者姓名),然後我會查看一篇文章的查詢和另外10篇關於相關作者信息的查詢。出於這個原因,我經常選擇加入。但是如果我把它搞糊塗了 - 更可能! ;-) - 那麼我會再給ZDT看看。 – 2012-02-20 06:45:10
我真的沒有分析關係調用,所以我不確定。它確實感覺有點沉重,所以你可能對它的作用是正確的。 – RockyFord 2012-02-21 03:10:02
我的意思是我仍然使用相同的DBTable類(用於後端)的前端數據操作和顯示? – ktm 2012-02-20 04:16:59
@Rajendra我認爲我們正在進行循環討論。我使用向控制器和視圖提供數據的dbTable發佈了一個編輯。 – RockyFord 2012-02-20 05:02:06