• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • As we have now upgraded the main XenForo community, this forum is now being retired. It will remain open in a mostly read only state. Any further feedback, suggestions or bug reports should be posted on the main forum. We will still process the existing bug reports from this forum.

    When we approach a stable release, this forum will become inaccessible. If there is any content you wish to save, please do so before then.

Search Function for entity

#1
I'm looking for how to do this with the entity

PHP:
protected function _postSave()
{
   $userDw = XenForo_DataWriter::create('XenForo_DataWriter_User');
   $userDw->setExistingData($this->get('user_id'));
   $userDw->set('username', $this->get('new_username'));
   $userDw->save();
}
;
@Chris D
 
#2
Have you considered searching the XF2 source code?

PHP:
\XF::em()->find('XF:User', $userId);
There's multiple examples of this throughout the source code, would probably have taken less than 1h40m to find the information as well :)


Fillip
 

MaGeFH

New Member
#4
To change the username it would be:

PHP:
$user = \XF::em()->findOne('XF:User', ['user_id' => $this->filter('user_id', 'uint')]);

if ($user)
{
    $user->username = $this->filter('username', 'str');
    $user->save();
}
 

Chris D

XenForo Developer
Staff member
#5
To change the username it would be:

PHP:
$user = \XF::em()->findOne('XF:User', ['user_id' => $this->filter('user_id', 'uint')]);

if ($user)
{
    $user->username = $this->filter('username', 'str');
    $user->save();
}
That can be made slightly shorter; the entity manager has an alternative method for finding an entity by its primary key alone:
PHP:
$user = \XF::em()->find('XF:User', $this->filter('user_id', 'uint'));

if ($user)
{
    $user->username = $this->filter('username', 'str');
    $user->save();
}
It will also pull the entity from the entity cache without an additional query if it has already been cached.