Skip to content

Commit

Permalink
add guidOrId method.
Browse files Browse the repository at this point in the history
  • Loading branch information
vistart committed Apr 11, 2017
1 parent bd06cb7 commit 6b2bf48
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions traits/EntityQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace rhosocial\base\models\traits;

use rhosocial\base\helpers\Number;

/**
* This trait is used for building entity query class for entity model.
*
Expand All @@ -21,7 +23,7 @@
trait EntityQueryTrait
{
use QueryTrait;

public $noInitModel;

/**
Expand All @@ -34,7 +36,7 @@ public function buildNoInitModel()
$this->noInitModel = $modelClass::buildNoInitModel();
}
}

/**
* Specify guid attribute.
* @param string|array $guid
Expand All @@ -46,7 +48,7 @@ public function guid($guid, $like = false)
$model = $this->noInitModel;
return $this->likeCondition((string)$guid, $model->guidAttribute, $like);
}

/**
* Specify id attribute.
* @param string|integer|array $id
Expand All @@ -58,7 +60,24 @@ public function id($id, $like = false)
$model = $this->noInitModel;
return $this->likeCondition($id, $model->idAttribute, $like);
}


/**
* Specify GUID or ID attribute.
* Scalar parameter is acceptable only.
* Please do not pass an array to the first parameter.
* @param string|integer $param
* @param bool|string $like false, 'like', 'or like', 'not like', 'or not like'.
* @return $this
*/
public function guidOrId($param, $like = false)
{
$model = $this->noInitModel;
if (is_string($param) && (preg_match(Number::GUID_REGEX, $param) || strlen($param) == 16)) {
return $this->guid($param, $like);
}
return $this->id($param, $like);
}

/**
* Specify create time range.
* @param string $start
Expand All @@ -74,7 +93,7 @@ public function createdAt($start = null, $end = null)
}
return static::range($this, $model->createdAtAttribute, $start, $end);
}

/**
* Specify order by creation time.
* @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
Expand All @@ -89,7 +108,7 @@ public function orderByCreatedAt($sort = SORT_ASC)
}
return $this->addOrderBy([$model->createdAtAttribute => $sort]);
}

/**
* Specify update time range.
* @param string $start
Expand All @@ -105,7 +124,7 @@ public function updatedAt($start = null, $end = null)
}
return static::range($this, $model->updatedAtAttribute, $start, $end);
}

/**
* Specify order by update time.
* @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
Expand All @@ -120,7 +139,7 @@ public function orderByUpdatedAt($sort = SORT_ASC)
}
return $this->addOrderBy([$model->updatedAtAttribute => $sort]);
}

public static $pageAll = 'all';
public static $defaultPageSize = 10;

Expand Down

0 comments on commit 6b2bf48

Please sign in to comment.