Reputation: 6085
I an using zend db table models for backend crud operations. However I think the model like this is meaningless for my front end data display like news by category , news and blog widgets and etc from various table or joining various table.
class Bugs extends Zend_Db_Table_Abstract {
protected $_name = 'bugs'; }
Model this way is perfect for my backend admin panel crud operation, How would i create model for front end operation, any example would be highly appreceated. Thanks
Upvotes: 0
Views: 211
Reputation: 14184
You can join on other tables even with a Zend_Db_Table
-derived model. Just be sure to turn off integrity check.
Code (not tested) could look something like this:
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);
}
}
Then in your controller:
$authorId = $this->_getParam('authorId');
$newsModel = new My_Model_News();
$this->view->articles = $newsModel->fetchNewsByAuthor($authorId);
Truth be told, that's one of the things that leaves me flat about most TableGateway approaches, like Zend_Db_Table
. I find that TableGateway is great for single-table queries, but I find that most of my real-life situations require multi-tables. As a result, I end up creating models that are not tied to a single table, but rather accept a Zend_Db_Adapter
instance and then query/join whatever tables they need. Or, I push out to more complex ORM's, like Doctrine.
Upvotes: 2
Reputation: 8519
I think what you are asking about in ZF would be a View Helper, A view helper takes data from your models and allows you to dump it to the view without having to process it in the controller. Here is a simple example:
<?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;
}
}
view helpers are characterized by returning some data (object, string, array, boolean) and can be used to supply data to views and partials.
The following is an example of a partial that uses several view helpers to present data in view.
<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>
and finally I call this partial in a view with:
<?php echo $this->partial('_dates.phtml', $this->memberData) ?>
as far as DbTable models being useless for the frontend, you might be surprised. Once you have established the relationships between your tables properly in your DbTable classes the functionality of what they can do goes way up. However if you are like most people you will likely have at least one layer of domain models (mappers, service, repository) between your DbTable classes and your application.
This is a model with relationships, it's sole purpose is to supply the data to build navigation.
<?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 supplies the interface for several data access patterns and you just have to supply the business logic and whatever level of abstraction you want.
Upvotes: 1