<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> <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> <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> Source: https://habr.com/ru/post/29466/
All Articles