Zend Framework Models
Posted: Tue Nov 04, 2008 3:27 am
It seems to me that I'm writing a hell of a lot of repetitive code in the models directory. Each model provides an interface to a specific database table and any associated tables right. But the actual classes are mere copies of one another bar protected $_name references a different table and if I have defined any convenience methods to make database queries simpler for me. Here is one of my model classes:
My question is this. Why not just create a single model that does all the database queries in a generic format. So, for instance, I would create a setter function for $_name. Would that break all MVC patterns? Is it a better method of coding on the framework to create multiple models doing, essentially, the same thing. Seems to me that this way violates DRY methods, for obvious reason.
Code: Select all
<?php
/** Class::Industry
* @author Feighen Oosterbroek
* @author feighen@noondaysun.org
* @copyright 2008 onwards Feighen Daniel Oosterbroek ta Noon Day Sun Programming
* @license Creative Commons BSD license
* @license http://creativecommons.org/licenses/BSD/ -> Summary License
* @license http://opensource.org/licenses/bsd-license.php
*/
class Industry extends Zend_Db {
//: Variables
protected $_name = "industry";
//: Public Functions
/** Industry::create($data)
* insert Data into the database
* @param array $data data to insert
* @return array results on success false otherwise
*/
public function create(array $data) {
$registry = Zend_Registry::getInstance();
$config = $registry->get("config");
$db = parent::factory($config->database);
$data["create_at"] = date("U");
$data["update_at"] = date("U");
$data["authorid"] = $config->user->id;
if (($db->insert($this->_name, $data)) === false) {
return false;
}
return self::read(array("id"=>$db->lastInsertId()));
}
/** Industry::delete($data)
* delete Data from the database
*/
public function delete($where) {
$registry = Zend_Registry::getInstance();
$config = $registry->get("config");
$db = parent::factory($config->database);
if (($db->delete($this->_name, $where)) === false) {
return false;
}
return true;
}
/** Industry::fetch($data)
* fetch Data from the database
* @param $qry[fields] string sql field list
* @param $qry[from] string sql table list
* @param $qry[where] string sql where statement
* @param $qry[group] string sql group by statement
* @param $qry[having] string sql having statement
* @param $qry[order] string sql order by statement
* @param $qry[limit] int sql limit
* @param $qry[start_row] int sql offset
* @return array results on success, false on failure
*/
public function read(array $data) {
$registry = Zend_Registry::getInstance();
$config = $registry->get("config");
$db = parent::factory($config->database);
# first check to see if $qry[fields] isset
# second check to see if $qry[from] isset
if (!isset($data["fields"]) || !$data["fields"]) {$data["fields"] = "*";}
if (!isset($data["from"]) || !$data["from"]) {$data["from"] = $this->_name;}
# we need to generate the qry_string
$qry_str = (string)"select ".$data["fields"];
$qry_str .= " from ".$data["from"];
$qry_str .= " where 1=1";
if (isset($data["where"]) && $data["where"]) {
$qry_str .= " and ".$data["where"];
}
if (isset($data["id"]) && $data["id"]) {
$qry_str .= " and id=".$data["id"];
}
if (isset($data["group"]) && $data["group"]) {
$qry_str .= " group by ".$data["group"];
}
if (isset($data["having"]) && $data["having"]) {
$qry_str .= " having ".$data["having"];
}
if (isset($data["order"]) && $data["order"]) {
$qry_str .= " order by ".$data["order"];
}
if (isset($data["limit"]) && $data["limit"]) {
$qry_str .= " limit ".$data["limit"];
if (isset($data["start_row"]) && $data["start_row"]) {
$qry_str .= " offset ".$data["start_row"];
}
}
if (($stmt = $db->query($qry_str)) === false) {
return false;
}
$func = isset($data["id"]) && $data["id"] ? "fetchRow" : "fetchAll";
return $stmt->$func();
}
/** Industry::update($data, $where)
* fetch Data from the database
*/
public function update(array $data, $where) {
$registry = Zend_Registry::getInstance();
$config = $registry->get("config");
$db = parent::factory($config->database);
$data["update_at"] = date("U");
if (($db->update($this->_name, $data, $where)) === false) {
return false;
}
return self::read(array("where"=>$where));
}
}