📜 ⬆️ ⬇️

Using Zend_Db_Table

At work, I had to deal with my own production ORM, which I did not like very much. I began to make my own (well, is it not a fool, eh? :)), in 3 days I got a simple ORM displaying the structure of tables on objects without controlling the types. The result was something like this:




Sample usage:
  <code class = "php"> $ table = new ArticleTable ();
 $ record = $ table-> fetchOneWhere ("slug = 'hello'");  // get the existing entry
 $ record-> name = 'Fucking Article!';
 $ record-> save ();  // calls insert / update depending on whether this is a new entry.
 $ record = $ table-> create ();  // create a new record
 $ record-> name = 'Fucking Article2!';
 $ record-> slug = 'fucking_article';
 // ...
 $ record-> save (); </ code> 

And something reminded me very much, namely - Zend_Db: Zend_Db_Table / Zend_Db_Table_Row. Without thinking twice, I threw my system out of my nafig and uploaded a piece of Zend Framework to the project (if necessary, then I’ll tell you exactly which files are needed to complete the work of the entire Zend_Db component), and also decided to read what now in general there is in this Zend_Db as it turned out - quite a lot:

In fact - there are still things that could be added to make them work automatically:

Look like that's it. The overall impression is just a great system. It is easy and pleasant to use. :)
')
Example:
First my supertypes of the layer will go (who read Patters of EAA will understand):
  <code class = "php"> class Db_Table extends Zend_Db_Table_Abstract {
   / **
    * @return Zend_Db_Table_Rowset_Abstract
    * /
   public function fetchAllBy ($ key, $ value) {
     $ where = $ this-> getAdapter () -> quoteInto ("$ key =?", $ value);
     return $ this-> fetchAll ($ where);
   }

   / **
    * @return Zend_Db_Table_Row_Abstract
    * /
   public function fetchRowBy ($ key, $ value) {
     $ where = $ this-> getAdapter () -> quoteInto ("$ key =?", $ value);
     return $ this-> fetchRow ($ where);
   }

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

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

     throw new Exception ("Undefined method $ name");
   }
 }

 class Db_Record extends Zend_Db_Table_Row_Abstract {
 } </ code> 

And now - an example of use:
  <code class = "php"> class Item extends Db_Table {
   protected $ _name = 'items';
   protected $ _rowClass = 'ItemRecord';
   protected $ _referenceMap = array (
       'Group' => array (
         'columns' => 'groupid',
         'refTableClass' => 'Group',
         'refColumns' => 'groupid',
       )
     );
 }

 class ItemRecord extends Db_Record {
 }

 class Group extends Db_Table {
   protected $ _name = 'groups';
   protected $ _rowClass = 'GroupRecord';
   protected $ _dependentTables = array ('Item');
 }

 class GroupRecord extends Db_Record {
 }

 $ itemTable = new Item ();
 $ item = $ itemTable-> fetchRowBySlug ('hello');
 $ group = $ item-> findParentGroup (); </ code> 

The coordinator is simple and convenient, isn't it?

For those interested, I strongly advise you to fully study the chapter on Zend_Db from the documentation of Zend Framework. And also - my post about Zend_Db_Table , dedicated to its improvement (although I do not know how relevant it is now, there is no time to check :().

Cross post from my blog

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


All Articles