Add SqlCriteria class. Add SqlModel.find() with use SqlCriteria.
This commit is contained in:
@ -15,7 +15,6 @@
|
||||
*/
|
||||
abstract class SqlModel extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $ident
|
||||
* @return string Quoted identifier.
|
||||
@ -177,5 +176,36 @@ abstract class SqlModel extends Model
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $criteria SqlCriteria
|
||||
*/
|
||||
public function find($criteria)
|
||||
{
|
||||
foreach ($criteria->select as $field => &$term) {
|
||||
if (is_int($field)) {
|
||||
$term = $this->db->quoteIdentifier($term);
|
||||
} else {
|
||||
$term = $this->db->quoteIdentifier($field) . ' as ' . $this->db->quoteIdentifier($term);
|
||||
}
|
||||
}
|
||||
foreach ($criteria->where as $cond => &$term) {
|
||||
$term = $this->db->quoteInto($cond, $term);
|
||||
}
|
||||
foreach ($criteria->order as $field => $term) {
|
||||
if (!is_int($field) && $term == 'desc') {
|
||||
$term = $term = $this->db->quoteIdentifier($field) . ' DESC';
|
||||
} else {
|
||||
$term = $this->db->quoteIdentifier($term);
|
||||
}
|
||||
}
|
||||
if ($criteria->distinct != '') {
|
||||
$criteria->distinct = 'DISTINCT ' . $this->db->quoteIdentifier($criteria->distinct);
|
||||
}
|
||||
$sql = 'SELECT ' . (($criteria->distinct) ? $criteria->distinct : implode(',', $criteria->select))
|
||||
. ' FROM ' . $this->db->quoteIdentifier($this->table)
|
||||
. (($criteria->where) ? (' WHERE ' . implode(' AND ', $criteria->where)) : '')
|
||||
. (($criteria->order) ? (' ORDER BY ' . implode(',', $criteria->order)) : '')
|
||||
. (($criteria->limit) ? (' LIMIT ' . implode(',',array_map(function($item){return intval($item);},explode(',',$criteria->limit)))) : '');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user