⬆️ ⬇️

Accelerate the development of part 1 (We expand Zend_Db_Table)

Good day. Many would agree that the Zend Framework is a great tool that allows you to greatly reduce project development time (and not only), but anyway you often have to do copy-paste methods in different places (controllers, models, etc.) . One of these places is the database table model.



Zend_Db_Table makes it easy to perform CRUD operations.



So, what actions when writing, for example, the module for CMS, we perform constantly?

')



So let's get started.



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 item.

public function addItem($data){

if (empty($data)) {

throw new Exception( " " );

}

return $ this ->insert($data);

}




* This source code was highlighted with Source Code Highlighter .




Now we will create a method for updating the item by the id key field.

public function updateItemById($id, $data = NULL){

if (empty($id)) {

throw new Exception( " id " );

}



if (empty($data)) {

throw new Exception( " " );

}



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

return $ this ->update($data, $ where );

}




* This source code was highlighted with Source Code Highlighter .




Method of deleting an item by id key field

public function deleteItemById($id){

if (empty($id)) {

throw new Exception( " id " );

}

$result = $ this ->delete(array( 'id = ?' => ( int )$id));

if (0 === $result) {

throw new Exception( " " );

}

}




* This source code was highlighted with Source Code Highlighter .




Element selection method by id key field

public function getItemById($id){

if (empty($id)) {

throw new Exception( " id " );

}

$ select = $ this ->getAdapter()->quoteInto( 'id = ?' , ( int )$id);

$result = $ this ->fetchRow($ select );

if (NULL !== $result) {

return $result->toArray();

} else {

throw new Exception( " " );

}

}




* This source code was highlighted with Source Code Highlighter .




And for convenience, we will use the getItemsBy magic method, 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();

if (count($result) > 0) {

return $result;

} else {

throw new Exception( " " );

}

}



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( " " .$name);

}




* This source code was highlighted with Source Code Highlighter .




How to use it now? Create a test table model (applicaiton / models / DbTable / Test.php) extending the class we created, where all the methods we have written will be available

class Model_DbTable_Test extends App_Db_Table

{

protected $_name = 'test' ;

}




* This source code was highlighted with Source Code Highlighter .




Controller example

// Test

$model = new Model_DbTable_Test();



// id = 1

try {

$item = $model->getItemById(1);

}

catch (Exception $e){

print $e->getMessage();

}



// getItemsBy()

try {

$item = $model->getItemsByName( 'itemname' );

}

catch (Exception $e){

print $e->getMessage();

}




* This source code was highlighted with Source Code Highlighter .




These are just a few example methods that can be taken out separately for frequent use. You can also write magic methods for updating and deleting items, for managing directory branches, etc.



PS don't forget to add library / App for auto-loading of classes, for example in the .ini config

autoloaderNamespaces [] = "App_"

Source: https://habr.com/ru/post/71553/



All Articles