class User <ActiveRecord :: Base named_scope: active,: conditions => {: active => true} named_scope: inactive,: conditions => {: active => false} named_scope: male,: conditions => {: sex => male} end
User.active # selects all active users User.inactive # Selects all inactive users User.active.male # Selects all active men
class dbTable_User_Select extends Zend_Db_Table_Select { public function status ($ status) { return $ this-> where ('status =?', $ status); } public function sex ($ sex) { return $ this-> where ('sex =?', $ sex); } public function hasAvatar () { return $ this-> where ('avatar is not null'); } public function sortById () { return $ this-> order ('id DESC); } }
$ user_table-> select () -> hasAvatar () -> sex ('male'); $ user_table-> select () -> status ('active') -> sortById ();
class Ext_Db_Table_Select extends Zend_Db_Table_Select { public function fetchRow () { return $ this-> getTable () -> fetchRow ($ this); } public function fetchRowIfExists ($ message = 'This page is not on the site') { return $ this-> getTable () -> fetchRowIfExists ($ this, $ message); } public function fetchAll ($ limit = null, $ offset = null) // Not the most successful method name) { if ($ limit) { $ this-> limit ($ limit, $ offset); } return $ this-> getTable () -> fetchAll ($ this); } public function getPaginator ($ page = 1, $ limit = 10, $ pageRange = 7) { $ adaptee = new Ext_Paginator_AdapterAggregate ($ this); $ paginator = Zend_Paginator :: factory ($ adaptee); $ paginator-> setItemCountPerPage ($ limit); $ paginator-> setCurrentPageNumber ($ page); $ paginator-> setPageRange ($ pageRange); return $ paginator; } // These useful methods can also be implemented. public function count () public function max ($ field) {}; public function min ($ field) {}; public function sum ($ field) {}; public function exists () {}; public function random ($ limit = 5) // Pseudo random { $ count = $ this-> count (); $ offset = ($ count> $ limit)? $ count - $ limit: 0; $ this-> limit ($ limit, mt_rand (0, $ offset)); return $ this-> fetchAll (); } }
$ select-> fetchRow (); // Get the string $ select-> getPaginator ($ page); // Paging $ select-> fetchAll (5); // take 5 lines // Often needed where, with an empty result, you need to throw 404 $ select-> fetchRowIfExists ();
Source: https://habr.com/ru/post/98877/
All Articles