Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Allow to disable CREATED_AT #1071

Closed
TBlindaruk opened this issue Mar 21, 2018 · 5 comments
Closed

[Proposal] Allow to disable CREATED_AT #1071

TBlindaruk opened this issue Mar 21, 2018 · 5 comments

Comments

@TBlindaruk
Copy link

TBlindaruk commented Mar 21, 2018

Problem

When we try to use only UPDATED_AT, without CREATED_AT,
then we have an error:

"message": "Type error: Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 525 and exactly 2 expected",

class Email{
   const CREATED_AT = null;
   const UPDATED_AT = 'updated';
}

Related problem:

issue related to the problem with UPDATED_AT to null

laravel/framework#21045

https://github.com/laravel/framework/pull/21178/files -PR for disable UPDATED_AT


Solution:

My solution will help to solve the problem with disable CERATED_AT column in the same way as in the previous PR for UPDATED_AT


So, I have already created the PR for 5.6 laravel/framework#23638 and it was closed.
One more PR with this proposal - laravel/framework#22990

Could we push this PR to the 5.7?

@steveheinsch
Copy link

steveheinsch commented Mar 21, 2018

Why not just make your own updated_at timestamp ($table->timestamp('updated_at');) field and not use $table->timestamps()?

If you're using mysql, it will add the CURRENT_TIMESTAMP for the default value, and also on update CURRENT_TIMESTAMP unless you set it to ->nullable() in the migration ($table->timestamp('updated_at')->nullable()).

@TBlindaruk
Copy link
Author

TBlindaruk commented Mar 21, 2018

@steveheinsch ,
It not for do not use $table->timestamps().

This proposition for allowing auto setting "updated_at" without 'created_at'.

I have created a few Unit tests for it:

<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Carbon;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentTimestamps extends TestCase
{
    public function setUp()
    {
        $db = new DB;

        $db->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');
    }

    /**
     * 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';
}

Now for 3 test case (testUserWithUpdatedAt) we got the error:

Illuminate\Tests\Database\DatabaseEloquentTimestamps::testUserWithUpdatedAt
ArgumentCountError: Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in
/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 548 and exactly 2 expected

@TBlindaruk
Copy link
Author

laravel/framework#23648 - merged in 5.7

@steveheinsch
Copy link

Not sure why you'd store the timestamp as a string: $table->string('updated_at');

@TBlindaruk
Copy link
Author

TBlindaruk commented Mar 22, 2018

@steveheinsch , It is just an example,
You can store the timestamp - it doesn't matter

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants