Переделал базовую модель - getAll на PDO
This commit is contained in:
parent
5958f6b614
commit
a8733fefbc
|
|
@ -142,28 +142,33 @@ abstract class Model
|
|||
* @param array $params ['where'] просто строка с условиями: (`id` > 1) AND (`name` == 'TEST'); ['order'] строка с именем толбца и напрабления: `summ` DESC
|
||||
* @return array с объектами
|
||||
*/
|
||||
function getAll($params = array())
|
||||
function getAll($params = array(), $values = array())
|
||||
{
|
||||
$sql = 'SELECT * FROM `' . $this->_tableName_ . '`';
|
||||
$sql .= isset($params['where']) && $params['where'] ? ' WHERE ' . $params['where'] : '';
|
||||
$sql .= isset($params['order']) && $params['order'] ? ' ORDER BY ' . $params['order'] : '';
|
||||
if (isset($params['limit']) && $params['limit']) {
|
||||
if (isset($params['offset']) && $params['offset']) {
|
||||
$sql .= ' LIMIT ' . $params['offset'] . ', ' . $params['limit'];
|
||||
$sql .= isset($params['useIndex']) ? ' USE INDEX(' . $params['useIndex'] . ')' : '';
|
||||
$sql .= isset($params['where']) ? ' WHERE ' . $params['where'] : '';
|
||||
$sql .= isset($params['order']) ? ' ORDER BY ' . $params['order'] : '';
|
||||
if (isset($params['limit']) && intval($params['limit'])) {
|
||||
if (isset($params['offset']) && intval($params['offset'])) {
|
||||
$sql .= ' LIMIT ' . intval($params['offset']) . ', ' . intval($params['limit']);
|
||||
} else {
|
||||
$sql .= ' LIMIT ' . $params['limit'];
|
||||
$sql .= ' LIMIT ' . intval($params['limit']);
|
||||
}
|
||||
}
|
||||
$res = App::DB()->query($sql);
|
||||
|
||||
$res = App::DB()->prepare($sql);
|
||||
$res->execute($values);
|
||||
|
||||
$ready = array();
|
||||
|
||||
$calledClass = get_called_class();
|
||||
while ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) {
|
||||
if (isset($params['noKeys'])) {
|
||||
$ready[] = isset($params['asArray']) && $params['asArray'] ? $row : new $calledClass($row);
|
||||
$element = isset($params['asArray']) && $params['asArray'] ? $row : new $calledClass($row);
|
||||
|
||||
if (isset($params['noKeys']) && $params['noKeys']) {
|
||||
$ready[] = $element;
|
||||
} else {
|
||||
$ready[$row[$this->_primaryKey_]] = isset($params['asArray']) && $params['asArray'] ? $row : new $calledClass($row);
|
||||
$ready[$row[$this->_primaryKey_]] = $element;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue