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.

84 lines
1.7 KiB

  1. <?php
  2. class SqlCriteria
  3. {
  4. private $select = array();
  5. private $distinct = '';
  6. private $where = array();
  7. private $order = array('sort' => array(), 'order' => array());
  8. private $limit = '';
  9. /**
  10. * @var SqlModel
  11. */
  12. private $model;
  13. public function find()
  14. {
  15. }
  16. /**
  17. * @param $cond string Condition with "?" placeholder @ex 'field=?'
  18. * @param $value string|array|DbExpr Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
  19. * @return SqlCriteria
  20. * @desc Allow multiple calls
  21. */
  22. public function where($cond, $value)
  23. {
  24. $this->where[$cond] = $value;
  25. return $this;
  26. }
  27. /**
  28. * @param $field string Field @ex 'field'
  29. * @param $order_desc bool Descendant sort direction
  30. * @return SqlCriteria
  31. * @desc Allow multiple calls
  32. */
  33. public function order($field, $order_desc = false)
  34. {
  35. $this->order['sort'][] = $field;
  36. if ($order_desc) {
  37. $this->order['order'][$field] = 'desc';
  38. }
  39. return $this;
  40. }
  41. /**
  42. * @param $limit int
  43. * @param $offset int
  44. * @return SqlCriteria
  45. */
  46. public function limit($limit, $offset = 0)
  47. {
  48. if ($offset) {
  49. $this->limit = (int) $offset . ',';
  50. }
  51. $this->limit .= (int) $limit;
  52. return $this;
  53. }
  54. /**
  55. * @param string $field
  56. * @return SqlCriteria
  57. */
  58. public function select($field)
  59. {
  60. $this->select[] = $field;
  61. return $this;
  62. }
  63. /**
  64. * @param $field string
  65. * @return SqlCriteria
  66. */
  67. public function distinct($field)
  68. {
  69. $this->distinct = $field;
  70. return $this;
  71. }
  72. }