diff --git a/app/PagerAction.php b/app/PagerAction.php index 1dad1ec..957db46 100644 --- a/app/PagerAction.php +++ b/app/PagerAction.php @@ -11,22 +11,22 @@ class PagerAction extends Action { - public $page; - public $last_page; + public $page = 1; + public $last_page = 1; protected $offset = 0; - protected $count = 0; protected $limit; - public function __construct($count, $limit = 20) + public function __construct($limit = 20) { - $this->count = $count; $this->limit = $limit; parent::__construct(); } - protected function execute() + protected function execute() {} + + public function setCount($count) { - $this->last_page = ceil($this->count / $this->limit); + $this->last_page = ceil($count / $this->limit); if (Env::Get('p') == 'last') { $page = $this->last_page; } else { @@ -38,12 +38,12 @@ class PagerAction extends Action public function getOffset() { - return $this->offset; + return (int) $this->offset; } public function getLimit() { - return $this->limit; + return (int) $this->limit; } protected function getTemplate() diff --git a/model/Model.php b/model/Model.php index 85df5ee..dee4e7a 100644 --- a/model/Model.php +++ b/model/Model.php @@ -139,6 +139,45 @@ abstract class Model implements iCacheable } return $autoindent ? $this->identify($this->table) : $this->table; } + + /** + * Creates order sql string + * + * @param array $params + * @param array $sortable + * @return string + */ + protected function order($params, $sortable = array('id')) + { + $sql = ''; + if (isset($params['sort'])) { + $order = (isset($params['order']) && $params['order'] == 'desc') ? 'DESC' : 'ASC'; + if (in_array($params['sort'], $sortable)) { + $sql = ' ORDER BY ' . $this->identify($params['sort']) . ' ' . $order; + } + } + return $sql; + } + + /** + * Searches using like + * + * @param array $params + * @param array $searchable + * @param string $table_prefix + * @return string + */ + protected function search($params, $searchable = array('id'), $table_prefix = '') + { + $sql = ''; + if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) { + if ($table_prefix) { + $sql = $table_prefix . '.'; + } + $sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%'); + } + return $sql; + } /** * @return Cache @@ -238,4 +277,4 @@ abstract class Model implements iCacheable } return $result; } -} \ No newline at end of file +} diff --git a/model/MySQLiDriver.php b/model/MySQLiDriver.php index 565c588..4afa22c 100644 --- a/model/MySQLiDriver.php +++ b/model/MySQLiDriver.php @@ -122,4 +122,4 @@ class MySQLiDriver extends DbDriver $this->connection->rollback(); $this->connection->autocommit(true); } -} \ No newline at end of file +} diff --git a/model/MySQLiStatement.php b/model/MySQLiStatement.php index 183d3a4..54f3cb0 100644 --- a/model/MySQLiStatement.php +++ b/model/MySQLiStatement.php @@ -91,11 +91,12 @@ class MySQLiStatement extends DbStatement $result = $mysqli->query($sql); } if ($result === false) { - throw new Exception($mysqli->error, $mysqli->errno); + $message = $mysqli->error . "\nQuery: \"" . $sql . '"'; + throw new Exception($message, $mysqli->errno); } if ($result instanceof MySQLi_Result) { $this->result = $result; } return true; } -} \ No newline at end of file +}