Accelerating the development of h 1 (Rishiram Zend_Db_Table)

Hello. Many will agree that Zend Framework is a great tool that allows you greatly reduce the development time of the project (and not only), but it's still often necessary to do copy-paste methods in different places (controllers, models, etc.). One such place is the model of a database table.

/ > Zend_Db_Table and so makes it easy to perform CRUD operations.

So what are the steps in writing, such as a module for the CMS we perform constantly?

the
    the
  • Add an item
  • the
  • Select items by any field
  • the
  • Delete the element
  • the
  • Update item

So let's start.

Create a class library/App/Db/Table.php
<?php
class App_Db_Table extends Zend_Db_Table_Abstract
{

}


* This source code was highlighted with Source Code Highlighter.


Create a method to add an element
public function addItem($data){
if(empty($data)) {
throw new Exception("No data to add");
}
return $this->insert($data);
}


* This source code was highlighted with Source Code Highlighter.


Now create a method to update the item by the key field id
public function updateItemById($id, $data = NULL){
if(empty($id)) {
throw new Exception("you Must specify the id of the element updated");
}

if(empty($data)) {
throw new Exception("No data to update");
}

$where = $this->getAdapter()->quoteInto('id = ?' (int),$id);
return $this->update($data, $where);
}


* This source code was highlighted with Source Code Highlighter.


Removal of the item by the key field id
public deleteItemById function($id){
if(empty($id)) {
throw new Exception("you Must specify the id of a deleted element");
}
$result = $this->delete(array('id = ?' => (int)$id));
if(0 === $result) {
throw new Exception("Element not found");
}
}


* This source code was highlighted with Source Code Highlighter.


The sampling method of the item by the key field id
public function getItemById($id){
if(empty($id)) {
throw new Exception("you Must specify an id for the select");
}
$select = $this->getAdapter()->quoteInto('id = ?' (int),$id);
$result = $this->fetchRow($select);
if(NULL !== $result) {
return $result->toArray();
}else{
throw new Exception("Element not found");
}
}


* This source code was highlighted with Source Code Highlighter.


And for convenience, we use a magic method getItemsBy, which will allow you to select elements by the value of any field.
public function getItemsBy($key, $value){
$where = $this->getAdapter()->quoteInto("$key = ?" $value);
$result = $this->fetchAll($where)->toArray();
return $result;
}else{
throw new Exception("Elements not found");
}
}

public function __call($name, $arguments) {
if(0 === strpos($name, 'getItemsBy')) {
array_unshift($arguments, substr($name, 10));
return call_user_func_array(array($this 'getItemsBy'), $arguments);
}

throw new Exception("Unknown method ".$name);
}


* This source code was highlighted with Source Code Highlighter.


Now how to use it? Create a table model test (applicaiton/models/DbTable/Test.php) rasshiryaya we have created the class, listing all of the written methods
class Model_DbTable_Test extends App_Db_Table
{
protected $_name = 'test';
}


* This source code was highlighted with Source Code Highlighter.


An example for the controller
// Create a model object table Test
$model = new Model_DbTable_Test();

// Select the element with id = 1
try{
$item = $model->getItemById(1);
}
catch (Exception $e){
print $e->getMessage();
}

// Choose the element using the magic methods getItemsBy()
try{
$item = $model->getItemsByName('itemname');
}
catch (Exception $e){
print $e->getMessage();
}


* This source code was highlighted with Source Code Highlighter.


These are only a few methods for example, which can be taken separately for frequent use. You can also write magic methods to update and delete items to manage the branches directory, etc.

P. S. don't forget to add library/App to avtopogruzchik classes, for example .ini configuration file
autoloaderNamespaces[] = "App_"
Article based on information from habrahabr.ru

Популярные сообщения из этого блога

Approval of WSUS updates: import, export, copy

The Hilbert curve vs. Z-order

Configuring a C++ project in Eclipse for example SFML application