php - When using Kohana DB, how does one avoid duplicate code when needing a count for pagination? -
php - When using Kohana DB, how does one avoid duplicate code when needing a count for pagination? -
using kohana query builder, possible build query piece piece.
then execute count on said query.
then execute query itself.
all without having write duplicate conditionals... 1 count , 1 results...
adding
db::select(array('count("pid")', 'mycount'))
to main query results in getting 1 record.
is maybe possible execute count, , somehow remove
array('count("pid")', 'mycount')
from select...
right way can think of find , replace on sql code itself, , running said code sql... there must improve way though... hope...
thanks!
to utilize 3 methods. first 1 returns paginated results, sec 1 gets count. third, private method, holds mutual conditions used #1 , #2. if query needs utilize joins or wheres or that, goes #3. way there no need repeat query.
/* 1 */ public function get_stuff($pagination = false){ $query = db::select(/* ... columns here ... */); $query = $this->get_stuff_query($query); if($pagination) $query->limit($pagination->items_per_page)->offset($pagination->offset); homecoming $query->execute(); } /* 2 */ public function get_stuff_count(){ $query = db::select(array('count("id")', 'total_rows')); $query = $this->get_stuff_query($query); $result = $query->execute(); homecoming $result->get('total_rows',0); } /* 3 */ private function get_stuff_query($query){ $query->from(/* tablename */); $query->join(/* ... */); $query->where(/* ... */); homecoming $query; }
php kohana kohana-3 kohana-db
Comments
Post a Comment