The Use Of Zend_Db_Table
On the job had to face a very not like me ORM'hell of its own production. Began to make his (well, not stupid, huh? :)), stuff 3 days a simple ORM that displays the table structure for objects, not kontroliruya types. The result turned out like this:
the
Approximate usage:
the
And something I'm very strongly reminded, namely, Zend_Db: Zend_Db_Table / Zend_Db_Table_Row. Without thinking — threw nafig their system and filled in the draft piece of Zend Framework's (if needed — then tell me what files are needed for the component Zend_Db) and decided to read that now even have this in Zend_Db, and there it was — quite a lot:
the
Actually — there are still things kotrye you could add that they work automatically:
the
Like all. Overall impression — just a wonderful system. Use easy and pleasant. :)
the Example:
First, go to my layer supertypes (those who have read Patters of EAA — you will understand):
the
And now — example usage:
the
Soglashatel — everything is simple and convenient, isn't it?
For those interested — I advise you to completely peruse the Chapter on Zend_Db from the documentation of Zend Framework's. And also my post about Zend_Db_Table dedicated to improving it (though I don't know how it is now relevant to see the time :( ).
Cross post from my blog
Article based on information from habrahabr.ru
the
-
the
- class database (unasledovala from mysqli, pdo was impossible to use) the
- class table stored in the database, and is responsible for CRUD of the records the
- class entry that redirects the CUD methods to a table class
Approximate usage:
the
<code class="php">$table = new ArticleTable(); $record = $table->fetchOneWhere("slug = 'hello'"); // get the existing record $record->name = 'Fucking Article!'; $record->save(); // call insert/update depending on whether this is a new record $record = $table->create(); // create a new entry $record->name = 'to view the article2 in Fucking!'; $record->slug = 'fucking_article'; // ... $record->save();</code>
And something I'm very strongly reminded, namely, Zend_Db: Zend_Db_Table / Zend_Db_Table_Row. Without thinking — threw nafig their system and filled in the draft piece of Zend Framework's (if needed — then tell me what files are needed for the component Zend_Db) and decided to read that now even have this in Zend_Db, and there it was — quite a lot:
the
-
the
- Good abstraction work with databases the
- Classes records/tables the
- Support fetch'Inga related objects the
- Support for many-to-many relations (even this is in no Propel)
Actually — there are still things kotrye you could add that they work automatically:
the
-
the
- Validators depending on types of the table fields the
- the Ability to immediately fetch'it data from multiple tables (more precisely, to obtain this information quite easily, but to scatter them to different objects and associate those objects now have handles, if I'm not mistaken, but again — it wouldn't be a problem)
Like all. Overall impression — just a wonderful system. Use easy and pleasant. :)
the Example:
First, go to my layer supertypes (those who have read Patters of EAA — you will understand):
the
<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 — example usage:
the
<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>
Soglashatel — everything is simple and convenient, isn't it?
For those interested — I advise you to completely peruse the Chapter on Zend_Db from the documentation of Zend Framework's. And also my post about Zend_Db_Table dedicated to improving it (though I don't know how it is now relevant to see the time :( ).
Cross post from my blog