Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * @author Ezekiel Fernandez <ezekiel_p_fernandez@yahoo.com>
- *
- * allows the user to created nested criterias on the fly
- *
- */
- class ExtendedDbCriteria extends CDbCriteria {
- /**
- * @var ExtendedDbCriteria contains the parent of the subcriteria
- */
- public $parentCriteria;
- /**
- * @var string contains the operators used by {@link ExtendedDbCriteria::endSubCriteria}
- */
- public $parentCriteriaOperator;
- /**
- * @param string $operator operator
- * @return ExtendedDbCriteria
- */
- public function beginSubCriteria($operator = 'AND'){
- $childCriteria = new self;
- $childCriteria->parentCriteria = $this;
- $childCriteria->parentCriteriaOperator = $operator;
- return $childCriteria;
- }
- /**
- *
- * @return ExtendedDbCriteria
- */
- public function endSubCriteria(){
- $this->parentCriteria->mergeWith($this,$this->parentCriteriaOperator);
- return $this->parentCriteria;
- }
- private $_clauseProperty = array('WHERE' => 'condition', 'HAVING' => 'having');
- public function addBetweenCondition($column, $valueStart, $valueEnd, $operator = 'AND', $clause = 'WHERE') {
- if ($valueStart === '' || $valueEnd === '')
- return $this;
- $paramStart = self::PARAM_PREFIX . self::$paramCount++;
- $paramEnd = self::PARAM_PREFIX . self::$paramCount++;
- $this->params[$paramStart] = $valueStart;
- $this->params[$paramEnd] = $valueEnd;
- $condition = "$column BETWEEN $paramStart AND $paramEnd";
- return $this->addCondition($condition, $operator, $clause);
- }
- public function addCondition($condition, $operator = 'AND', $clause = 'WHERE') {
- if (is_array($condition)) {
- if ($condition === array())
- return $this;
- $condition = '(' . implode(') ' . $operator . ' (', $condition) . ')';
- }
- if ($this->{$this->_clauseProperty[$clause]} === '')
- $this->{$this->_clauseProperty[$clause]} = $condition;
- else
- $this->{$this->_clauseProperty[$clause]} = '(' . $this->{$this->_clauseProperty[$clause]} . ') ' . $operator . ' (' . $condition . ')';
- return $this;
- }
- public function addInCondition($column, $values, $operator = 'AND', $clause = 'WHERE') {
- if (($n = count($values)) < 1)
- $condition = '0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE
- elseif ($n === 1) {
- $value = reset($values);
- if ($value === null)
- $condition = $column . ' IS NULL';
- else {
- $condition = $column . '=' . self::PARAM_PREFIX . self::$paramCount;
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
- }
- } else {
- $params = array();
- foreach ($values as $value) {
- $params[] = self::PARAM_PREFIX . self::$paramCount;
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
- }
- $condition = $column . ' IN (' . implode(', ', $params) . ')';
- }
- return $this->addCondition($condition, $operator, $clause);
- }
- public function addColumnCondition($columns, $columnOperator = 'AND', $operator = 'AND', $clause = 'WHERE') {
- $params = array();
- foreach ($columns as $name => $value) {
- if ($value === null)
- $params[] = $name . ' IS NULL';
- else {
- $params[] = $name . '=' . self::PARAM_PREFIX . self::$paramCount;
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
- }
- }
- return $this->addCondition(implode(" $columnOperator ", $params), $operator, $clause);
- }
- public function addNotInCondition($column, $values, $operator = 'AND', $clause = 'WHERE') {
- if (($n = count($values)) < 1)
- return $this;
- if ($n === 1) {
- $value = reset($values);
- if ($value === null)
- $condition = $column . ' IS NOT NULL';
- else {
- $condition = $column . '!=' . self::PARAM_PREFIX . self::$paramCount;
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
- }
- } else {
- $params = array();
- foreach ($values as $value) {
- $params[] = self::PARAM_PREFIX . self::$paramCount;
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
- }
- $condition = $column . ' NOT IN (' . implode(', ', $params) . ')';
- }
- return $this->addCondition($condition, $operator, $clause);
- }
- public function addSearchCondition($column, $keyword, $escape = true, $operator = 'AND', $like = 'LIKE', $clause = 'WHERE') {
- if ($keyword == '')
- return $this;
- if ($escape)
- $keyword = '%' . strtr($keyword, array('%' => '\%', '_' => '\_', '\\' => '\\\\')) . '%';
- $condition = $column . " $like " . self::PARAM_PREFIX . self::$paramCount;
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $keyword;
- return $this->addCondition($condition, $operator, $clause);
- }
- public function compare($column, $value, $partialMatch = false, $operator = 'AND', $escape = true, $clause = 'WHERE') {
- if (is_array($value)) {
- if ($value === array())
- return $this;
- return $this->addInCondition($column, $value, $operator, $clause);
- }
- else
- $value = "$value";
- if (preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/', $value, $matches)) {
- $value = $matches[2];
- $op = $matches[1];
- }
- else
- $op = '';
- if ($value === '')
- return $this;
- if ($partialMatch) {
- if ($op === '')
- return $this->addSearchCondition($column, $value, $escape, $operator, 'LIKE', $clause);
- if ($op === '<>')
- return $this->addSearchCondition($column, $value, $escape, $operator, 'NOT LIKE', $clause);
- }
- elseif ($op === '')
- $op = '=';
- $this->addCondition($column . $op . self::PARAM_PREFIX . self::$paramCount, $operator, $clause);
- $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
- return $this;
- }
- public function compareHaving($column, $value, $partialMatch = false, $operator = 'AND', $escape = true) {
- return $this->compare($column, $value, $partialMatch, $operator, $escape, 'HAVING');
- }
- public function addHavingSearchCondition($column, $keyword, $escape = true, $operator = 'AND', $like = 'LIKE') {
- return $this->addSearchCondition($column, $keyword, $escape, $operator, $like, 'HAVING');
- }
- public function addHavingNotInCondition($column, $values, $operator = 'AND') {
- return $this->addNotInCondition($column, $values, $operator, 'HAVING');
- }
- public function addHavingInCondition($column, $values, $operator = 'AND') {
- return $this->addInCondition($column, $values, 'HAVING');
- }
- public function addHavingBetweenCondition($column, $valueStart, $valueEnd, $operator) {
- return $this->addBetweenCondition($column, $valueStart, $valueEnd, $operator, 'HAVING');
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement