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

Import dateCreated and lastLoginDate for users #1388

Open
outline4 opened this issue Nov 30, 2023 · 2 comments
Open

Import dateCreated and lastLoginDate for users #1388

outline4 opened this issue Nov 30, 2023 · 2 comments
Labels

Comments

@outline4
Copy link

Description

In our case the dateCreated of our users is significant. I was trying to import it into users, but there's no such field to map.

It would be great if we could import the dateCreated and lastLoginDate.
It's the two field you display in the user listing, so they seem rather important.

Additional info

  • Craft version: Craft Pro 4.5.11.1
  • PHP version: 8.2.12
  • Database driver & version: MariaDB 10.5.23
  • FeedMe: 5.3.0
@outline4 outline4 added the bug label Nov 30, 2023
@jrrdnx
Copy link

jrrdnx commented Oct 2, 2024

@outline4 For what it's worth, I was able to update this information after the initial element is saved in a custom module event handler:

use craft\db\Table;
use craft\elements\User;
use craft\feedme\events\FeedProcessEvent;
use craft\feedme\services\Process;
use craft\helpers\Db;
use DateTime;
use yii\base\Event;

Event::on(
    Process::class,
    Process::EVENT_STEP_AFTER_ELEMENT_SAVE,
    function(FeedProcessEvent $event) {
        if($event->element instanceof User) {
            // Grab dateCreated and lastLoginDate timestamps
            $dateCreated = (new DateTime)->setTimestamp($event->feedData['join_date'])->format('Y-m-d H:i:s');
            $lastLoginDate = (new DateTime)->setTimestamp($event->feedData['last_visit'])->format('Y-m-d H:i:s');

            // Update `craft_users`
            Db::update(Table::USERS, [
                'pending' => 1,
                'passwordResetRequired' => 1,
                'dateCreated' => $dateCreated,
                'lastLoginDate' => $lastLoginDate,
            ], [
                'id' => $event->element->id
            ]);

            // Update `craft_elements`
            Db::update(Table::ELEMENTS, [
                'dateCreated' => $dateCreated,
            ], [
                'id' => $event->element->id
            ]);
        }
});

$event->feedData gives you access to everything in the import source file even if it's not used by Feed Me. Can ignore pending and passwordResetRequired if necessary, we're doing this as a one-time import from an older system. I'm also using this method to import Addresses as well.

@outline4
Copy link
Author

outline4 commented Oct 3, 2024

Thanks a lot!

I will certainly report back when I try it the next time!

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

No branches or pull requests

2 participants