From ded89940a0dbdab71633a1a0164a46a9617a359d Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Wed, 21 Mar 2018 01:12:22 +0200 Subject: [PATCH 1/3] [5.6] Allow to disable `CREATED_AT`. --- src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php b/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php index ce3e0801dee6..a9077058f584 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php @@ -42,7 +42,7 @@ protected function updateTimestamps() $this->setUpdatedAt($time); } - if (! $this->exists && ! $this->isDirty(static::CREATED_AT)) { + if (! $this->exists && ! is_null(static::CREATED_AT) && ! $this->isDirty(static::CREATED_AT)) { $this->setCreatedAt($time); } } From da9daa5807425a7ed7639ddf46de8835ed77a826 Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Wed, 21 Mar 2018 21:15:37 +0200 Subject: [PATCH 2/3] [5.6] add unit tests for CREATED_AT and UPDATED_AT disabling. --- tests/Database/DatabaseEloquentTimestamps.php | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/Database/DatabaseEloquentTimestamps.php diff --git a/tests/Database/DatabaseEloquentTimestamps.php b/tests/Database/DatabaseEloquentTimestamps.php new file mode 100644 index 000000000000..c2b817964e2a --- /dev/null +++ b/tests/Database/DatabaseEloquentTimestamps.php @@ -0,0 +1,152 @@ +addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + + $db->bootEloquent(); + $db->setAsGlobal(); + + $this->createSchema(); + } + + /** + * Setup the database schema. + * + * @return void + */ + public function createSchema() + { + $this->schema()->create('users', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + $table->timestamps(); + }); + + $this->schema()->create('users_created_at', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + $table->string('created_at'); + }); + + $this->schema()->create('users_updated_at', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + $table->string('updated_at'); + }); + } + + /** + * Tear down the database schema. + * + * @return void + */ + public function tearDown() + { + $this->schema()->drop('users'); + $this->schema()->drop('users_created_at'); + $this->schema()->drop('users_updated_at'); + } + + /** + * Tests... + */ + public function testUserWithCreatedAtAndUpdatedAt() + { + $now = Carbon::now(); + $user = UserWithCreatedAndUpdated::create([ + 'email' => 'test@test.com', + ]); + + $this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString()); + $this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString()); + } + + + public function testUserWithCreatedAt() + { + $now = Carbon::now(); + $user = UserWithCreated::create([ + 'email' => 'test@test.com', + ]); + + $this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString()); + } + + public function testUserWithUpdatedAt() + { + $now = Carbon::now(); + $user = UserWithUpdated::create([ + 'email' => 'test@test.com', + ]); + + $this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString()); + } + + /** + * Get a database connection instance. + * + * @return Connection + */ + protected function connection() + { + return Eloquent::getConnectionResolver()->connection(); + } + + /** + * Get a schema builder instance. + * + * @return Schema\Builder + */ + protected function schema() + { + return $this->connection()->getSchemaBuilder(); + } +} + +/** + * Eloquent Models... + */ +class UserWithCreatedAndUpdated extends Eloquent +{ + protected $table = 'users'; + + protected $guarded = []; +} + +class UserWithCreated extends Eloquent +{ + public const UPDATED_AT = null; + + protected $table = 'users_created_at'; + + protected $guarded = []; + + protected $dateFormat = 'U'; +} + +class UserWithUpdated extends Eloquent +{ + public const CREATED_AT = null; + + protected $table = 'users_updated_at'; + + protected $guarded = []; + + protected $dateFormat = 'U'; +} From a0843990621408a45cd338068f509f581e10d97b Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Fri, 23 Mar 2018 09:47:50 +0200 Subject: [PATCH 3/3] [5.6] code reformat. --- src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php b/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php index a9077058f584..8e3d488edd2d 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php @@ -42,7 +42,8 @@ protected function updateTimestamps() $this->setUpdatedAt($time); } - if (! $this->exists && ! is_null(static::CREATED_AT) && ! $this->isDirty(static::CREATED_AT)) { + if (! $this->exists && ! is_null(static::CREATED_AT) && + ! $this->isDirty(static::CREATED_AT)) { $this->setCreatedAt($time); } }