Skip to content

Commit

Permalink
Added createdAtToday and updatedAtToday methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vistart committed May 15, 2017
1 parent 36165a1 commit 48d6cb0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Yii 2 Base Models Change Log

- Enh: Added `SubsidiaryTrait::hasSubsidiary()` to check whether the entity has the subsidiary or not.
- Enh: Added `OperatorTrait` to record operator who operate the entity.
- Enh: Added `createdAtToday` and `updatedAtToday` methods to specify the creation time or last updated time as today.
26 changes: 26 additions & 0 deletions tests/entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,30 @@ public function testGuidOrId()
$this->assertNotEquals($model->getGUID(), $entities[8]->getGUID());
$this->assertNotEquals($model->getID(), $entities[8]->getID());
}

/**
* @group entity
* @group timestamp
*/
public function testCreatedAtToday()
{
$this->assertEquals(0, (int)Entity::find()->createdAtToday()->count());
$this->assertTrue($this->entity->save());
$entity = Entity::find()->createdAtToday()->one();
$this->assertEquals($this->entity->guid, $entity->guid);
$this->assertEquals(1, $this->entity->delete());
}

/**
* @group entity
* @group timestamp
*/
public function testUpdatedAtToday()
{
$this->assertEquals(0, (int)Entity::find()->updatedAtToday()->count());
$this->assertTrue($this->entity->save());
$entity = Entity::find()->updatedAtToday()->one();
$this->assertEquals($this->entity->guid, $entity->guid);
$this->assertEquals(1, $this->entity->delete());
}
}
54 changes: 47 additions & 7 deletions traits/EntityQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace rhosocial\base\models\traits;

use rhosocial\base\helpers\Number;
use rhosocial\base\models\models\BaseEntityModel;
use yii\db\ActiveQuery;

/**
* This trait is used for building entity query class for entity model.
Expand All @@ -24,6 +26,9 @@ trait EntityQueryTrait
{
use QueryTrait;

/**
* @var BaseEntityModel
*/
public $noInitModel;

/**
Expand All @@ -45,6 +50,7 @@ public function buildNoInitModel()
*/
public function guid($guid, $like = false)
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
return $this->likeCondition((string)$guid, $model->guidAttribute, $like);
}
Expand All @@ -57,6 +63,7 @@ public function guid($guid, $like = false)
*/
public function id($id, $like = false)
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
return $this->likeCondition($id, $model->idAttribute, $like);
}
Expand All @@ -71,69 +78,102 @@ public function id($id, $like = false)
*/
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.
* Specify creation time range.
* @param string $start
* @param string $end
* @return $this
*/
public function createdAt($start = null, $end = null)
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
/* @var $this yii\db\ActiveQuery */
if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
return $this;
}
return static::range($this, $model->createdAtAttribute, $start, $end);
}

/**
* Specify creation time as today (in locally).
* @return $this
*/
public function createdAtToday()
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
$start = strtotime(date('Y-m-d'));
$end = $start + 86400;
if ($model->timeFormat == BaseEntityModel::$timeFormatDatetime) {
$start = gmdate('Y-m-d H:i:s', $start);
$end = gmdate('Y-m-d H:i:s', $end);
}
return $this->createdAt($start, $end);
}

/**
* Specify order by creation time.
* @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
* @return $this
*/
public function orderByCreatedAt($sort = SORT_ASC)
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
/* @var $this yii\db\ActiveQuery */
if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
return $this;
}
return $this->addOrderBy([$model->createdAtAttribute => $sort]);
}

/**
* Specify update time range.
* Specify last updated time range.
* @param string $start
* @param string $end
* @return $this
*/
public function updatedAt($start = null, $end = null)
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
/* @var $this yii\db\ActiveQuery */
if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
return $this;
}
return static::range($this, $model->updatedAtAttribute, $start, $end);
}

/**
* Specify last updated time as today (in locally).
* @return $this
*/
public function updatedAtToday()
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
$start = strtotime(date('Y-m-d'));
$end = $start + 86400;
if ($model->timeFormat == BaseEntityModel::$timeFormatDatetime) {
$start = gmdate('Y-m-d H:i:s', $start);
$end = gmdate('Y-m-d H:i:s', $end);
}
return $this->updatedAt($start, $end);
}

/**
* Specify order by update time.
* @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
* @return $this
*/
public function orderByUpdatedAt($sort = SORT_ASC)
{
/* @var $this ActiveQuery */
$model = $this->noInitModel;
/* @var $this yii\db\ActiveQuery */
if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
return $this;
}
Expand Down

0 comments on commit 48d6cb0

Please sign in to comment.