Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly record the datetime for last_active #404

Merged
merged 1 commit into from
Aug 29, 2022

Conversation

datamweb
Copy link
Collaborator

@datamweb datamweb commented Aug 26, 2022

Hello friends,
correctly record the datetime for last_active in case of change defaultLocale.

Before:

+----+--------------------+--------+----------------+--------+--------------------+--------------------+--------------------+------------+
| id | username           | status | status_message | active | last_active        | created_at         | updated_at         | deleted_at |
+----+--------------------+--------+----------------+--------+--------------------+--------------------+--------------------+------------+
| 1  | pooyaparsadadas... |        |                | 1      | 0000-00-00 00:0... | 2022-08-25 15:3... | 2022-08-25 15:3... |            |
+----+--------------------+--------+----------------+--------+--------------------+--------------------+--------------------+------------+

After:

+----+--------------------+--------+----------------+--------+--------------------+--------------------+--------------------+------------+
| id | username           | status | status_message | active | last_active        | created_at         | updated_at         | deleted_at |
+----+--------------------+--------+----------------+--------+--------------------+--------------------+--------------------+------------+
| 1  | pooyaparsadadas... |        |                | 1      | 2022-08-25 15:4... | 2022-08-25 15:3... | 2022-08-25 15:3... |            |
+----+--------------------+--------+----------------+--------+--------------------+--------------------+--------------------+------------+

Ref #238

@datamweb datamweb added the bug Something isn't working label Aug 26, 2022
@datamweb datamweb marked this pull request as draft August 26, 2022 09:02
@kenjis
Copy link
Member

kenjis commented Aug 26, 2022

I think we've fixed this bug in the past.
Why again?

@datamweb
Copy link
Collaborator Author

datamweb commented Aug 26, 2022

@kenjis, This bug was created from #392.
But my question is, is the test written in Commit valid?

Oh, maybe you mean problem 0000-00-00 00:00:00.
Because SessionAuth is not enabled by default. In fact, when SessionAuth is not set, last_active will not be updated. So we didn't see it because it wasn't set.
I was checking 392 last night when I noticed this.

@datamweb
Copy link
Collaborator Author

@kenjis see here!!

public function recordActiveDate(): void
{
if (! $this->user instanceof User) {
throw new InvalidArgumentException(
__METHOD__ . '() requires logged in user before calling.'
);
}
-$this->user->last_active = Time::now()->format('Y-m-d H:i:s');
+$time = Time::now()->format('Y-m-d H:i:s');

+$this->user->last_active = $time;

+echo ($time);

+echo ($this->user->last_active);
+exit;

        $this->provider->updateActiveDate($this->user);
    }

output :

2022-08-26 23:10:12 and ۲۰۲۲-۰۸-۲۶ ۲۳:۱۰:۱۲ way?

Is it related to Handling Business Logic?!؟

Temporary solution:
$this->user->last_active = new Time('now','','en-US');

@datamweb
Copy link
Collaborator Author

datamweb commented Aug 26, 2022

oh! I noticed something.
If we delete:

'last_active',

And then

 $this->user->last_active = Time::now()->format('Y-m-d H:i:s');

The date is recorded without any problems!

See Here!

@kenjis
Copy link
Member

kenjis commented Aug 26, 2022

@datamweb Ah, you're correct! Date mutator converts date string to Time object.

This is a problem in the database layer.
So we should provide the database safe date string at
https://github.com/codeigniter4/shield/pull/392/files#diff-459f49e48ae4f20464cf08fd3256652536435610f4f7db89305fe7269e539ac9R329

@kenjis
Copy link
Member

kenjis commented Aug 26, 2022

But my question is, is the test written in Commit valid?

It is valid. last_active is updated after the creation of the object.

┌──────────────────────────────────────────────────────────────────────────────┐
│ auth(...)->user()->updated_at                                                │
└──────────────────────────────────────────────────────────────────────────────┘
CodeIgniter\I18n\Time (6) (
    protected 'timezone' -> DateTimeZone (2) (
        public 'timezone_type' -> integer 3
        public 'timezone' -> string (15) "America/Chicago"
    )
    protected 'locale' -> string (2) "en"
    protected 'toStringFormat' -> string (19) "yyyy-MM-dd HH:mm:ss"
    public 'date' -> string (26) "2022-08-26 16:12:13.000000"
    public 'timezone_type' -> integer 3
    public 'timezone' -> string (15) "America/Chicago"
)
┌──────────────────────────────────────────────────────────────────────────────┐
│ auth(...)->user()->last_active                                               │
└──────────────────────────────────────────────────────────────────────────────┘
CodeIgniter\I18n\Time (6) (
    protected 'timezone' -> DateTimeZone (2) (
        public 'timezone_type' -> integer 3
        public 'timezone' -> string (15) "America/Chicago"
    )
    protected 'locale' -> string (2) "en"
    protected 'toStringFormat' -> string (19) "yyyy-MM-dd HH:mm:ss"
    public 'date' -> string (26) "2022-08-26 16:12:13.119705"
    public 'timezone_type' -> integer 3
    public 'timezone' -> string (15) "America/Chicago"
)
════════════════════════════════════════════════════════════════════════════════

@MGatner
Copy link
Member

MGatner commented Aug 27, 2022

That was my faulty approval. I didn't realize that the $dates property actually stores a Time instance 🤦‍♂️ IMO this is a mistake, since the values in $attributes should be ready for persistence.

@kenjis
Copy link
Member

kenjis commented Aug 27, 2022

I didn't realize that the $dates property actually stores a Time instance 🤦‍♂️ IMO this is a mistake, since the values in $attributes should be ready for persistence.

I thought that. But if we convert it to string, it will lose the timezone.

@datamweb datamweb reopened this Aug 27, 2022
@datamweb
Copy link
Collaborator Author

I don't like to have a PR about 0000-00-00 00:00:00 again.
And what about this? is there a need to change?

protected $dates = [
'expires',
'last_used_at',
];

@datamweb datamweb marked this pull request as ready for review August 27, 2022 12:59
@datamweb datamweb requested a review from kenjis August 27, 2022 12:59
src/Entities/User.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member

kenjis commented Aug 28, 2022

The coding rule that we must follow now is:

  • When we use a DateTime column, we must pass database safe datetime string to the QueryBuilder

So the following code is incorrect, because it passes a Time object.

$time = Time::now();
$this->builder->set('last_active', $time)

But it works in locale en, etc. Because it will be converted to string like 2022-08-29 13:00:00 when it is cast to string.
But it will be '۲۰۲۲-۰۸-۲۹ ۱۳:۰۰:۰۰' in locale fa, and does not work.

I would like to fix it as a bug to work in all locales, because Time is a CI4 standard class.

@MGatner
Copy link
Member

MGatner commented Aug 28, 2022

We can continue this discussion on the framework issue, but just to reinforce what @kenjis said:

  • Query Builder statements should always use a string input for datetimes
  • Model/Entity interactions can use either strings or Time; by default Entities with $dates fields will cast to Time and Model will handle the string conversion

@datamweb datamweb merged commit 74cce7a into codeigniter4:develop Aug 29, 2022
@datamweb datamweb deleted the fix-datetime-last_active branch August 29, 2022 13:05
@datamweb
Copy link
Collaborator Author

Kenjis and MGatner Thanks for the review and useful information.
I merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants