Browse Source

Changed PagerAction, search and order methods of Model, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@148 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
master
pzinovkin 15 years ago
parent
commit
70ccc8bc26
  1. 18
      app/PagerAction.php
  2. 41
      model/Model.php
  3. 2
      model/MySQLiDriver.php
  4. 5
      model/MySQLiStatement.php

18
app/PagerAction.php

@ -11,22 +11,22 @@
class PagerAction extends Action class PagerAction extends Action
{ {
public $page;
public $last_page;
public $page = 1;
public $last_page = 1;
protected $offset = 0; protected $offset = 0;
protected $count = 0;
protected $limit; protected $limit;
public function __construct($count, $limit = 20)
public function __construct($limit = 20)
{ {
$this->count = $count;
$this->limit = $limit; $this->limit = $limit;
parent::__construct(); 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') { if (Env::Get('p') == 'last') {
$page = $this->last_page; $page = $this->last_page;
} else { } else {
@ -38,12 +38,12 @@ class PagerAction extends Action
public function getOffset() public function getOffset()
{ {
return $this->offset;
return (int) $this->offset;
} }
public function getLimit() public function getLimit()
{ {
return $this->limit;
return (int) $this->limit;
} }
protected function getTemplate() protected function getTemplate()

41
model/Model.php

@ -139,6 +139,45 @@ abstract class Model implements iCacheable
} }
return $autoindent ? $this->identify($this->table) : $this->table; 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 * @return Cache
@ -238,4 +277,4 @@ abstract class Model implements iCacheable
} }
return $result; return $result;
} }
}
}

2
model/MySQLiDriver.php

@ -122,4 +122,4 @@ class MySQLiDriver extends DbDriver
$this->connection->rollback(); $this->connection->rollback();
$this->connection->autocommit(true); $this->connection->autocommit(true);
} }
}
}

5
model/MySQLiStatement.php

@ -91,11 +91,12 @@ class MySQLiStatement extends DbStatement
$result = $mysqli->query($sql); $result = $mysqli->query($sql);
} }
if ($result === false) { 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) { if ($result instanceof MySQLi_Result) {
$this->result = $result; $this->result = $result;
} }
return true; return true;
} }
}
}
Loading…
Cancel
Save