Skip to content

Commit

Permalink
Improve the Database ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Jun 2, 2018
1 parent a3bcdb7 commit a33387b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/Database/ORM/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,10 @@ protected function getKeyForSaveQuery()
*/
public function touch()
{
if (! $this->timestamps) {
return false;
}

$this->updateTimestamps();

return $this->save();
Expand All @@ -1601,12 +1605,12 @@ protected function updateTimestamps()
{
$time = $this->freshTimestamp();

if (! $this->isDirty(static::UPDATED_AT)) {
$this->setUpdatedAt($time);
if (! is_null($column = $this->getUpdatedAtColumn()) && ! $this->isDirty($column)) {
$this->setAttribute($column, $time);
}

if (! $this->exists && ! $this->isDirty(static::CREATED_AT)) {
$this->setCreatedAt($time);
if (! $this->exists && ! is_null($column = $this->getCreatedAtColumn()) && ! $this->isDirty($column)) {
$this->setAttribute($column, $time);
}
}

Expand All @@ -1618,7 +1622,9 @@ protected function updateTimestamps()
*/
public function setCreatedAt($value)
{
$this->{static::CREATED_AT} = $value;
if (! is_null($column = $this->getCreatedAtColumn())) {
$this->setAttribute($column, $value);
}
}

/**
Expand All @@ -1629,7 +1635,9 @@ public function setCreatedAt($value)
*/
public function setUpdatedAt($value)
{
$this->{static::UPDATED_AT} = $value;
if (! is_null($column = $this->getUpdatedAtColumn())) {
$this->setAttribute($column, $value);
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Database/ORM/SoftDeletingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class SoftDeletingScope implements ScopeInterface
*
* @var array
*/
protected $extensions = ['ForceDelete', 'Restore', 'WithTrashed', 'OnlyTrashed'];
protected $extensions = array('ForceDelete', 'Restore', 'WithTrashed', 'OnlyTrashed');


/**
* Apply the scope to a given ORM query builder.
Expand Down Expand Up @@ -158,7 +159,7 @@ protected function addOnlyTrashed(Builder $builder)
*/
protected function isSoftDeleteConstraint(array $where, $column)
{
return $where['type'] == 'Null' && $where['column'] == $column;
return ($where['type'] == 'Null') && ($where['column'] == $column);
}

}
8 changes: 4 additions & 4 deletions src/Database/ORM/SoftDeletingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ protected function runSoftDelete()
{
$query = $this->newQuery()->where($this->getKeyName(), $this->getKey());

$this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp();
$this->setAttribute($column = $this->getDeletedAtColumn(), $time = $this->freshTimestamp());

$query->update(array($this->getDeletedAtColumn() => $this->fromDateTime($time)));
$query->update(array($column => $this->fromDateTime($time)));
}

/**
Expand All @@ -77,7 +77,7 @@ public function restore()
return false;
}

$this->{$this->getDeletedAtColumn()} = null;
$this->setAttribute($this->getDeletedAtColumn(), null);

//
$this->exists = true;
Expand All @@ -96,7 +96,7 @@ public function restore()
*/
public function trashed()
{
return ! is_null($this->{$this->getDeletedAtColumn()});
return ! is_null($this->getAttribute($this->getDeletedAtColumn()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Application extends Container implements ResponsePreparerInterface
*
* @var string
*/
const VERSION = '4.0.74';
const VERSION = '4.0.75';

/**
* Indicates if the application has "booted".
Expand Down

0 comments on commit a33387b

Please sign in to comment.