I'm learning FuelPHP and try to use OrmAuth to handle the authentication and authorisation processes. I was able to generate all the "standard" OrmAuth tables (user, users_permissions, users_metadata and so on). However, I don't understand what is the right way to add custom fields to the user object (e.g., telefon_number). Can someone help me out here and give some examples?
Thank you.
First, what you want is easily achievable with the EAV container already configured in your user model (from the OrmAuth package). You just have to set any metadata on the model, and save it, like that:
$user = Auth_User::find(1);
$user->telefon_number = '+36 30 taratatta-taratatta';
$user->save();
That'll put the telefon_number
in your users_metadata
table, but when you query a user, it'll be automatically available on that model instance.
However, sometimes that's just not sufficient, for example when you want to build advanced queries using these properties. Than you might want to have the field to be present in the users
table.
What I did to achieve this is documented under the extending the core part of the documentation. Basically I extended the \Auth\Model\Auth_User
class from the OrmAuth package, like this:
namespace Model;
class Auth_User extends \Auth\Model\Auth_User
{
/**
* @var array model properties
*/
protected static $_properties = [
// your properties go here
];
}
Now, in your app's bootstrap.php
, you have to tell the autoloader to use this class instead of the one in the OrmAuth package:
\Autoloader::add_classes(array(
// Add classes you want to override here
// Example: 'View' => APPPATH.'classes/view.php',
'Auth_User' => APPPATH.'classes/model/auth/user.php',
'Model\\Auth_User' => APPPATH.'classes/model/auth/user.php',
));
Note: This example sets both Auth_User
and Model\Auth_User
, but one might just be enough, depending on your needs.