Skip to content

Commit

Permalink
fix time
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuraovq committed Jul 11, 2019
1 parent c88e72e commit caadf0e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 56 deletions.
30 changes: 24 additions & 6 deletions src/db/src/Concern/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ public function attributesToArray()
return $attributes;
}

/**
* Get model attributes value
*
* @return array
*/
public function getModelAttributesValue(): array
{
return $this->modelAttributes;
}

/**
* Get an attribute array of all arrayable attributes.
*
* @return array
*/
protected function getArrayableAttributes()
public function getArrayableAttributes()
{
return array_merge($this->modelAttributes, $this->getModelAttributes());
}
Expand Down Expand Up @@ -276,13 +286,14 @@ protected function getArrayAttributeByKey($key)
/**
* Encode the given value as JSON.
*
* @param mixed $value
* @param $value
* @param int $option
*
* @return string
* @return false|string
*/
protected function asJson($value)
protected function asJson($value, $option = JSON_UNESCAPED_UNICODE)
{
return json_encode($value, JSON_UNESCAPED_UNICODE);
return json_encode($value, $option);
}

/**
Expand Down Expand Up @@ -577,11 +588,18 @@ public function getDirty()
{
$dirty = [];

foreach ($this->getArrayableAttributes() as $key => $value) {
foreach ($this->getModelAttributes() as $key => $value) {
if (!$this->originalIsEquivalent($key, $value)) {
$dirty[$key] = $value;
}
}

foreach ($this->modelAttributes as $key => $value) {
if ($value instanceof Expression || !$this->originalIsEquivalent($key, $value)) {
$dirty[$key] = $value;
}
}

return $dirty;
}

Expand Down
22 changes: 17 additions & 5 deletions src/db/src/Concern/HasTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,44 @@ public function touch(): bool
/**
* Update the creation and update timestamps.
*
* @return void
* @return array
*/
protected function updateTimestamps(): void
public function updateTimestamps(): array
{
$updated = [];

if (!is_null(static::UPDATED_AT) &&
!$this->isDirty(static::UPDATED_AT) &&
$this->hasSetter(static::UPDATED_AT)
) {
$this->setModelAttribute(static::UPDATED_AT, $this->freshTimestamp(static::UPDATED_AT));
$createTimestamp = $this->freshTimestamp(static::UPDATED_AT);

$this->setModelAttribute(static::UPDATED_AT, $createTimestamp);

$updated[static::UPDATED_AT] = $createTimestamp;
}

if (!$this->swoftExists &&
!is_null(static::CREATED_AT) &&
!$this->isDirty(static::CREATED_AT) &&
$this->hasSetter(static::CREATED_AT)
) {
$this->setModelAttribute(static::CREATED_AT, $this->freshTimestamp(static::CREATED_AT));
$createTimestamp = $this->freshTimestamp(static::CREATED_AT);

$this->setModelAttribute(static::CREATED_AT, $createTimestamp);

$updated[static::CREATED_AT] = $createTimestamp;
}

return $updated;
}

/**
* Get a fresh timestamp for the model.
*
* @param string $column
*
* @return false|int|string
* @return int|string
*/
public function freshTimestamp(string $column)
{
Expand Down
49 changes: 15 additions & 34 deletions src/db/src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,8 @@ public function updateOrCreate(array $attributes, array $values = [])
*/
public function updateOrInsert(array $attributes, array $values = [])
{
// Get safe values
$values = $this->model->getSafeAttributes($values, true);
// Update timestamp
$values = $this->addUpdatedAtColumn($values);
return $this->toBase()->updateOrInsert($attributes, $values);
$instance = $this->firstOrNew($attributes);
return $instance->fill($values)->save();
}

/**
Expand Down Expand Up @@ -838,6 +835,7 @@ protected function addUpdatedAtColumn(array $values): array

if (!$this->model->usesTimestamps() ||
!$this->model->hasSetter($updatedAtColumn) ||
$this->model->isDirty($updatedAtColumn) ||
is_null($updatedAtColumn)
) {
return $values;
Expand All @@ -846,49 +844,29 @@ protected function addUpdatedAtColumn(array $values): array
return $this->fillTimestampColumn($updatedAtColumn, $values);
}

/**
* Add the "create at" column to an array of values.
*
* @param array $values
*
* @return array
*/
protected function addCreatedAtColumn(array $values): array
{
$createdAtColumn = $this->model->getCreatedAtColumn();

if (!$this->model->usesTimestamps() ||
!$this->model->hasSetter($createdAtColumn) ||
is_null($createdAtColumn) ||
$this->model->getAttributeValue($createdAtColumn)
) {
return $values;
}

return $this->fillTimestampColumn($createdAtColumn, $values);
}

/**
* Fill timestamp column
*
* @param string $column
* @param array $values
*
* @return array
* @throws DbException
*/
private function fillTimestampColumn(string $column, array $values): array
{
$values = array_merge(
[$column => $this->model->freshTimestamp($column)],
$values
);
$values[$column] = $this->model->freshTimestamp($column);

// Update model field
$this->model->setModelAttribute($column, $values[$column]);

$segments = preg_split('/\s+as\s+/i', $this->query->from);

$qualifiedColumn = end($segments) . '.' . $column;

$values[$qualifiedColumn] = $values[$column];


unset($values[$column]);

return $values;
Expand Down Expand Up @@ -1042,7 +1020,8 @@ public function insertGetId(array $values, string $sequence = null): string
if (empty($values)) {
return '0';
}
$values = $this->addCreatedAtColumn($values);
$values = array_merge($values, $this->model->updateTimestamps());

return $this->toBase()->insertGetId($values, $sequence);
}

Expand All @@ -1065,12 +1044,14 @@ public function insert(array $values): bool
$values = [$values];
}
foreach ($values as &$item) {
$item = $this->model->getSafeAttributes($item, true);
$item = $this->addCreatedAtColumn($item);
$model = $this->model->setRawAttributes($item, true);

$item = array_merge($model->getModelAttributesValue(), $model->updateTimestamps());
}
unset($item);
// Filter empty values
$values = array_filter($values);

return $this->toBase()->insert($values);
}

Expand Down
9 changes: 0 additions & 9 deletions src/db/src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,6 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s
* @param string $method
*
* @return void
*/
/**
*
*
* @param string $column
* @param $amount
* @param $extra
* @param $method
*
* @throws DbException
*/
protected function incrementOrDecrementAttributeValue(string $column, $amount, $extra, $method)
Expand Down
50 changes: 48 additions & 2 deletions src/db/test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testSave()
'age' => mt_rand(1, 100),
'user_desc' => 'u desc'
];
$result3 = User::new($attributes)->save();
$result3 = User::new($attributes)->save();
$this->assertTrue($result3);

$batch = User::insert([
Expand All @@ -73,6 +73,20 @@ public function testSave()
'xxxx' => '223asdf'
]
]);

Count::insert([
[
'user_id' => 1,
'attributes' => uniqid(),
'create_time' => 111,
],
[
'user_id' => 2,
'attributes' => uniqid(),
'create_time' => 222,

],
]);
$this->assertTrue($batch);


Expand Down Expand Up @@ -576,13 +590,45 @@ public function testAutoJson()

public function testAutoTimestamp()
{
// create time
$count = Count::new();
$count->setUserId($this->addRecord());
$count->save();
$this->assertGreaterThan(0, $count->getCreateTime());
$this->assertGreaterThan(0, strtotime($count->getUpdateTime()));


$newCount = Count::find($count->getId());
$divTime = '2019-07-11 17:00:1';

$newCount->setUpdateTime($divTime);
// update time
$result = $newCount->update(['user_id' => 12233]);

$this->assertGreaterThan(0, $newCount->getCreateTime());
$this->assertTrue($result);
$this->assertEquals($divTime, $newCount->getUpdateTime());
$this->assertGreaterThan(0, strtotime($newCount->getUpdateTime()));
}

public function testUpdateEntity()
{
$count = Count::new(['create_time' => 0]);
$count->setAttributes("swoft");

$this->assertTrue($count->save());
$this->assertEquals('swoft', $count->getAttributes());
$this->assertEquals(0, $count->getCreateTime());
$this->assertEquals(time(), strtotime($count->getUpdateTime()));


$time = '2018-03-06 21:09:18';
$result = $count->fill([
'update_time' => $time,
'user_id' => Expression::new('`create_time` + 1'),
])->update();
$this->assertTrue($result);
$this->assertEquals($time, $count->getUpdateTime());
$this->assertEquals($time, Count::find($count->getId())->getUpdateTime());
$this->assertEquals(null, $count->getUserId());
}
}

0 comments on commit caadf0e

Please sign in to comment.