Updating Members records from extensions
Hi there, I hope someone has some time to help a newbie extension programmer out here...
I'm wanting to change the groups a user belongs to from inside the checkCredentials hook, while the user is logging in. I've tried updating the database directly using something like...
Code:
$this->Database->prepare("UPDATE tl_member SET groups = ? WHERE username = ?")...
but the changes always get overwritten again immediately after with the original values. I think further down the line in the login process the original record must be being written out again from the values in memory.
My question is simply, how can you change a user's groups (or for that matter any field) from inside an extension and have those changes reflected both internally in the User object in memory and on the database?
I realise this is probably painfully basic, but I've just spent two days trying everything out I could think of, and my knowledge just isn't up to the task :-(
Massive thanks in advance!!
Dubs
Re: Updating Members records from extensions
Try using the postLogin hook instead:
https://contao.org/en/manual/3.0/custom ... #postlogin
This hook passes the FrontendUser object, so you can probably make your changes directly to it instead of making your own database call:
Code:
public function myPostLoginHook(FrontendUser $objUser)
{
$objUser->allGroups = array(1,2,3); // Group IDs
$objUser->save();
}
I think something like that would work, just by looking at the API. It looks like $objUser->groups is temporary and won't get saved, but $objUser->allGroups would actually update the user record when saved. (I think). So that's why I'd recommend trying that instead.
http://api.contao.org/v2/
Re: Updating Members records from extensions
That's really helpful and makes a lot more sense than how I was trying to do it. I'd also missed the difference between the allGroups and groups variables. I've tried changing allGroups now and issuing a save() to the object and it's finally working.
Thanks, Brian!!