Save to table user

Robert9

Well-known member
I just try to save a field to the table user.
I do it like this

Code:
        // update user
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
        $dw->setExistingData($user);
        $dw->set('custom_title', "Test");
        $dw->save();

Now i want to do the same thing to xf_user.new_field
XF answers me that new_field could not be recognized.

How can i tell XF to know my new field, please?
 
You would have to add a new column to the MySql user table and then extend the user datawriter's _getFields() function for it to recognise it.
 
Meanwhile i use the trophy points for my needs, because we dont use them for points.
I have to write all fields of the user table in another file again?
I cant go on with some lines to write a field?
 
Not sure what you mean.
In MySql for example:
Code:
ALTER TABLE xf_user
ADD custom VARCHAR(15) NOT NULL DEFAULT

In PHP extend DataWriter_User:
PHP:
protected function _getFields()
{
   $fields = parent::_getFields();
   $fields['xf_user']['custom'] = array('type' => self::TYPE_STRING, 'maxLength' => 15, 'default' => '');
 
   return $fields;
}
 
I have seen this in many addons with a new file in /DataWriter/

My question is if i can do it just short and easy.

As long i use

// update user
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
$dw->setExistingData($user);
$dw->set('trophy_points', "1");
$dw->save();

i can use it just beneath another $dw_save() inside a file.

// update user
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');

do something to tell XF that there is another field

$dw->setExistingData($user);
$dw->set('custom', "1");
$dw->save();
 
You need to create a small add-on to do this as first you need to add your new "custom" column to the user table.
Then you need to let the datawriter know about this new column.
Only then can you fetch or save your new "custom" field using the datawriter.
The datawriter checks against the user table so will only accept existing columns.
 
Thank you, but that is not the problem. I have phpMyAdmin.

I also have seen how to add a new file with all the needed information in /DataWriter/
but i really look for the shortest version without any addon, without any files.
It is just to add one more update field into another addon that will not be updated in the next 100 years.

Somehow i should do the trick just here, right?

// update user
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');

do something to tell XF that there is another field

$dw->setExistingData($user);
$dw->set('custom', "1");
$dw->save();
 
No shortcuts in life I'm afraid.
You still need to extend the datawriter.
Takes 5 minutes to create a simple add-on to do that and it sounds like you already know how to write it but refuse to.
You say you want it as part of another add-on? Then just add the code to that add-on and re-install.
Good luck.
 
PHP:
$userDw = XenForo_DataWriter::create('XenForo_DataWriter_User', XenForo_DataWriter::ERROR_ARRAY);
$userDw->setExistingData($userId);
$userDw->set('thread_count',$userDw->get('thread_count') + 1);
$userDw->save();

This code is from my Thread Count add-on
Try with editing this
 
Thank you, but it is a normal field, right?
The problem is here to write to a new field that is not listed somewhere in XF.
 
Ok. Then i am shure that you have done a definition of this field somewhere. :)
A really dirty way to solve my problem are two lines in xenforo/DataWriter/User.php
Till the next update this will fix my problem.

But i still believe that there is another way, without an extra file or hardcode inside xf.
 
Ok. Then i am shure that you have done a definition of this field somewhere. :)
A really dirty way to solve my problem are two lines in xenforo/DataWriter/User.php
Till the next update this will fix my problem.

But i still believe that there is another way, without an extra file or hardcode inside xf.

You should extend DataWriter_User with this content

PHP:
protected function _getFields()
    {
        $parent = parent::_getFields();

        $parent['xf_user']['thread_count'] = array(
            'type' => self::TYPE_UINT, 'default' => 0
        );


        return $parent;
    }

For testing on your localhost, just download this add-on and dig its codes :)
 
@Robert9
You can use db query command with sql update codes.
Code:
$db->query("UPDATE xf_user
SET thread_count = 'something'
WHERE user_id =?", $userId);

So, you wont need to be extend datawriter.
Ps: didnt tried code, writing on phone, please check it again before using
 
Top Bottom