Add SqlCriteria class. Add SqlModel.find() with use SqlCriteria.
This commit is contained in:
86
model/SqlCriteria.php
Normal file
86
model/SqlCriteria.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
class SqlCriteria
|
||||
{
|
||||
public $select = array('*');
|
||||
|
||||
public $distinct = '';
|
||||
|
||||
public $where = array();
|
||||
|
||||
public $order = array();
|
||||
|
||||
public $limit = '';
|
||||
|
||||
/**
|
||||
* @return SqlCriteria
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $conditions array @ex array('field=?' => $value, 'field in ? => new DbExpr('(1,2,3)'))
|
||||
* @return SqlCriteria
|
||||
*/
|
||||
public function where($conditions)
|
||||
{
|
||||
$this->where = $conditions;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $fields array()
|
||||
* @param array $fields_to_desc
|
||||
* @return SqlCriteria
|
||||
*/
|
||||
public function order($fields, $fields_to_desc = array())
|
||||
{
|
||||
if (!is_array($fields)) {
|
||||
$fields = array($fields);
|
||||
}
|
||||
foreach ($fields as $term) {
|
||||
$term = $this->db->quoteIdentifier($term) . (in_array($term, $fields_to_desc) ? ' DESC' : '');
|
||||
}
|
||||
$this->order = implode(',', $fields);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $limit int
|
||||
* @param $offset int
|
||||
* @return SqlCriteria
|
||||
*/
|
||||
public function limit($limit, $offset = 0)
|
||||
{
|
||||
if (empty($offset)) {
|
||||
$this->limit = (int) $limit;
|
||||
}
|
||||
$this->limit = (int)$limit . ',' . (int) $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $select array @ex array('field1', 'field2', 'field3' => 'field')
|
||||
* @return string
|
||||
*/
|
||||
public function select($select)
|
||||
{
|
||||
if (!is_array($select)) {
|
||||
$select = array($select);
|
||||
}
|
||||
$this->select = implode(',', $select);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $field string
|
||||
* @return SqlCriteria
|
||||
*/
|
||||
public function distinct($field)
|
||||
{
|
||||
$this->distinct = $field;
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user