You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.0 KiB

<?php
class SqlCriteria
{
private $select = array();
private $distinct = '';
private $where = array();
private $order = array('sort' => array(), 'order' => array());
private $limit = '';
/**
* @var SqlModel
*/
private $model;
/**
* @param $model SqlModel
*/
public function __construct($model)
{
$this->model = $model;
}
/**
* @return SqlResultProvider
*/
public function find()
{
return $this->model->find($this->select, $this->distinct, $this->where, $this->order, $this->limit);
}
/**
* @param $cond string Condition with "?" placeholder @ex 'field=?'
* @param $value string|array|DbExpr Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
* @return SqlCriteria
* @desc Allow multiple calls
*/
public function where($cond, $value)
{
$this->where[$cond] = $value;
return $this;
}
/**
* @param $field string Field @ex 'field'
* @param $order_desc bool Descendant sort direction
* @return SqlCriteria
* @desc Allow multiple calls
*/
public function order($field, $order_desc = false)
{
$this->order['sort'][] = $field;
if ($order_desc) {
$this->order['order'][$field] = 'desc';
}
return $this;
}
/**
* @param $limit int
* @param $offset int
* @return SqlCriteria
*/
public function limit($limit, $offset = 0)
{
if ($offset) {
$this->limit = (int) $offset . ',';
}
$this->limit .= (int) $limit;
return $this;
}
/**
* @param string $field
* @return SqlCriteria
*/
public function select($field)
{
$this->select[] = $field;
return $this;
}
/**
* @param $field string
* @return SqlCriteria
*/
public function distinct($field)
{
$this->distinct = $field;
return $this;
}
}