0

It is 100% possible I am going about this the wrong way, so please feel free to correct me.

I have a registration form that creates a user. I have a plugin that is listening to the onSaveUser event:

MyPlugin.php

craft()->on('users.onSaveUser', function (Event $event) {
    if ($event->params['isNewUser']) {
        $entry = new EntryModel();
        $entry->sectionId = 16;
        $entry->typeId = 1;
        $entry->enabled = false;
        $entry->getContent()->title = craft()->request->getPost('firstName') . ' ' . craft()->request->getPost('lastName');

        ...

        if (craft()->entries->saveEntry($entry)) {
            // success
        } else {
            // trouble!
        }
    }  
});

Everything works great. The entries I am creating are basically an extension of the user's account (height, weight etc).

I know there are events for deleting a user, but what is the best way to relate the newly created entry so it gets deleted along with the user?

I thought about having an entries field on the actual user account that somehow I populate with the newly created entry?

EDIT

Or, should I have a user field on the newly created entry? In that case, I'm not sure how I populate the newly created entry with the newly created userID.

For example:

...
$user = $event->params['user'];
$entry = new EntryModel();
$entry->sectionId = 16;
$entry->typeId = 1;
$entry->enabled = false;
$entry->getContent()->title = craft()->request->getPost('firstName') . ' ' . craft()->request->getPost('lastName');

// account is the handle of my users field
$entry->getContent()->account = $user->id;  // im not setting this correctly

if (craft()->entries->saveEntry($entry)) {
    // success
} else {
    // trouble!
}

...
2
  • 1
    Posting as a comment, since this is off the top of my head... I think you want to set authorId of your entry to be the new user's id. Then, when you delete a user (through the control panel), it'll ask "What do you want to do with this person's content?"... at that point, you can just select "Delete it". If this actually helps you solve the problem, I'll post it as an official answer. :)
    – Lindsey D
    Commented Dec 3, 2015 at 5:41
  • BRILLIANT! I forgot all about that dialog - works perfectly! Update your comment to the answer and I'll mark it. Thanks Lindsey
    – Damon
    Commented Dec 4, 2015 at 1:54

1 Answer 1

2

You'll want to set the authorId of your entry to be the new user's id.

When you delete that user through the control panel, it'll ask "What do you want to do with this person's content?"... At that point, you can just select "Delete it".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.