XF 2.2 How to update an existing row in a table in the database?

Gobb

Member
Hi there!

In XF 1 we simply had Datawriters and the setExistingData facility which enabled you to very easily update an existing row in the database. I've looked through the documentation and the files but for the life of me cannot find how this is done? It seems to be done via formAction and then basicEntitySave, but I'm not having much luck in an addon's controller.

I can easily get the update to work extending the Account public controller save, for example. But if I'm not extending a controller and it's an addon's controller, I don't understand how I'm meant to be able to update a value in the xf_user table, for example?

Any insight will be much appreciated,

Thanks.
 
As a starting point, you generally want to be working with entities. You can fetch entities from a controller easily using their short class name and primary key:

PHP:
/** @var \XF\Entity\User $user */
$user = $this->em()->find('XF:User', $userId);
$user->column = 'value';
$user->save();

Depending on your exact use case, you may want to use a FormAction or service as an intermediate rather than updating them from the controller directly, but that should get you going.
 
As a starting point, you generally want to be working with entities. You can fetch entities from a controller easily using their short class name and primary key:

PHP:
/** @var \XF\Entity\User $user */
$user = $this->em()->find('XF:User', $userId);
$user->column = 'value';
$user->save();

Depending on your exact use case, you may want to use a FormAction or service as an intermediate rather than updating them from the controller directly, but that should get you going.
Hey Mate, thanks for the quick response! I must've glossed over the find facility of entities, thanks a lot!
 
Top Bottom