Adding data to the user table during registration via an add-on

Chris D

XenForo developer
Staff member
Ok, so I'm extending the Register controller and actionRegister. This is a very basic version of what I'm doing...

PHP:
$parent = parent::actionRegister();
 
$dataId = MyAddOn_Helper_MyAddOnHelper::getDataId();
 
$writer = $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
$writer->set('myaddon_data', $dataId);
$writer->save();
 
return $parent;

I've extended the User Data Writer so that the _getFields function includes the myaddon_data field.

One of two things happens...

If $writer->save is in the code then I get some errors - understandable as presumably I'm trying to write a row to the xf_user table that doesn't have some crucial fields set yet, such as user_id and other required fields.

If I remove $writer->save I get no errors, but the data isn't saved at all.

I'm sure I'm missing something that will solve this :cautious:

Help is much appreciated :D
 
Thanks for that.

Not sure if it will help me though because I'm trying to add this data as a user account is being created so there isn't an existing record to check for.

Any ideas how I could handle that?
 
I dealt with a similar situation in the Nodes As Tabs addon, where I have to write a tab record by node_id when creating a new forum. I simply query the latest node_id in my extended controller:

Code:
				$db = XenForo_Application::get('db');
				$nodeData['node_id'] = $db->fetchOne("
					SELECT node_id
					FROM xf_node
					ORDER BY node_id
					DESC
				");

That will be the node_id of the most recently inserted node.

There is also some discussion here:

http://xenforo.com/community/threads/get-id-of-new-thread-from-actionaddthread-in-my-class.29849/
 
Isn't $writer returned with the new user id?

After it does it's save, you should be able to get the new user id with this:
$userId = $writer->get('user_id');

and then use that to update the new user's fields for that user
 
That is true, but because of the way I am extending the Register action, all of my code is happening before the parent code therefore all of my code is happening before the $writer is creating a record. Therefore no user_id to get.

I ran out of time last night (read: needed to go to bed before my eyes burst into flames) but I had it pretty much working.

Essentially what I'm doing is adding a unique record to the Data Registry which contains the new user's e-mail address and the data I want to store, then I'm extending the Post Save function in my extended DataWriter so that we then get the record from the Data Registry, update the relevant field and then remove the record from the Data Registry.

If this proves to be unreliable, I may look at doing what I need to do AFTER the parent code has run in the Register action but I'm pretty sure when I tried that before it cocked up some other add-on so I want to be careful.
 
Back
Top Bottom