Skip to content

Commit

Permalink
Merge pull request #20 from karllhughes/master
Browse files Browse the repository at this point in the history
Updating model and test to fix laravel timestamps
  • Loading branch information
duxet committed Dec 22, 2015
2 parents 397ed11 + 5eb20b6 commit 1a6c54d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace duxet\Rethinkdb\Eloquent;

use Carbon\Carbon;
use DateTime;
use duxet\Rethinkdb\Eloquent\Relations\BelongsTo;
use duxet\Rethinkdb\Query\Builder as QueryBuilder;
Expand Down Expand Up @@ -32,15 +31,41 @@ public function getQualifiedKeyName()
}

/**
* Return DateTime object as Carbon instance.
* Ensure Timestamps are returned in DateTime.
*
* @param DateTime $value
* @param \DateTime $value
*
* @return \Carbon\Carbon
* @return \DateTime
*/
protected function asDateTime($value)
{
return Carbon::instance($value);
if ($value instanceof DateTime) {
return $value;
}

return new DateTime($value);
}

/**
* Retain DateTime format for storage.
*
* @param \DateTime $value
*
* @return string
*/
public function fromDateTime($value)
{
return $this->asDateTime($value);
}

/**
* Get a fresh timestamp for the model.
*
* @return \DateTime
*/
public function freshTimestamp()
{
return new DateTime();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,15 @@ public function testMultipleOr()
})->get();
$this->assertEquals(2, count($users));
}

public function testCreatedAt()
{
$users = User::orderBy('created_at', 'desc')->take(10)->get();
$prev_date = new \DateTime();
foreach ($users as $user) {
$this->assertEquals('DateTime', get_class($user->created_at));
$this->assertLessThanOrEqual($prev_date, $user->created_at);
$prev_date = $user->created_at;
}
}
}

0 comments on commit 1a6c54d

Please sign in to comment.