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!
}
...
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. :)