Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to use the Auth_User model to create a sign up form with custom profile fields?
  • I have a sign up page that I want to use the Auth_User model with. I have a custom set of profile fields that I want to add to my form. I was able to create the form manually and also get the from fields and save to the database using the Auth::create_user. 

    This is not something I want to do I want to be able to put my custom profile fields to the $_properties attribute and be able to generate the from with validation (using FieldSet probably). So in hope of doing that, I tried to extend the Auth_User model as Model_User in my application and then tired to override the $_properties and added a new entry 'profile_fields' which is an array containing all the custom fields. Not surprisingly it didn't work. How do I achieve that?


  • HarroHarro
    Accepted Answer
    What are "your custom profile fields"?

    Ormauth (since you mention the Auth_User model) doesn't use profile fields at all, it uses a separate metadata table to store user related information, which is accessed from the User object through an EAV container.

    Suppose you have a user with a profile field "fullname", you can just access that as any other property, using $userobject->fullname. You can not use this method to dynamically add new properties, this is on the roadmap. If you need to add something to an existing user object, you have to add a new metadata object to the "metadata" relation.

    To create users, always use the create_user() method, and not the save() method of the model. If you do, there will not be any Auth specific validation, the users password will not be hashed properly, and therefore the created account will not work.
  • My Custom profile fields are, for example, first_name, last_name, address etc. I was able to figure out how OrmAuth uses a separate metadata table. I was also able to save my custom fields to that metadata table by using Auth::create_user() method and providing my custom fields as an array to it. 

    However, what I'm looking for is a way to automatically (automagic was something I got used to working with Rails but I just read that it's not something FuelPHP sees in high esteem as opposed to the RoR crowd) do the form creation and validation using FieldSet by somehow attaching my new property values to the model and giving the model as whole to the form generator. Yeah, I'm that lazy :)

    All silliness aside, I guess I can do the validations manually and then use the create_user method to  save my model and do further Auth specific validation, such as, checking if the email already exists etc. I just asked this question to find out if there is a better way of doing this. 

    Thanks for the help!

     
  • Fuel can do some automagic, but there are limits.

    There is no way the Fieldset can detect what else you would like to have in there, and you can't use the model's properties for that, because they are reserved for table colums. A property that doesn't map to a column will have the ORM throw an exception...

    The usual and most transparent way is to add a method to your model called 'set_form_fields' (check the ORM model class for the prototype). It is called by the Fieldset when you do add_model(), and is passed the fieldset object. You can use that to customize the fieldset after you have called parent::set_form_fields to do the initial population.

    It allows you to add extra fields, disable fields from generating, add or change rules or attributes, etc.
  • I checked out your suggestion and due to my very limited knowledge of the framework, I opted for doing the validation manually. I'm creating the form manually as well, it's a one-off thing so it's not a big deal. 

    The only problem I have now is that I'm unable to use Auth::user_update to update one of my  custom fields, I saw the typo correction you made here, https://github.com/fuel/auth/commit/4eeaab154eaa61423d930617d6b73973be640d62 , the very next line to that, $current_values->is_changed(), is not picking up my changes made to the one of the custom fields and the change I made is not reflecting in the metadata database table. What could be the problem? Here is how I used the update method.

    Auth::update_user(
                           array(
                               "is_active" => true,
                           )
                       );

  • With the current (1.7/develop) Ormauth Login driver, that should work. It assumes additional data passed are metadata values, but since they are set in exactly the same way as model properties, they would be set correctly.

    With Simpleauth additional columns in the user table won't work, it will add all additional fields to the profile_fields array.
  • I'm on 1.6 and I'm using Ormauth. I tried it with out the is_changed() method check and it works as expected. Should I go ahead and replace my Model file in the Auth package with the one from 1.7? Or if I need to update to 1.7 entirely please give me the instructions to do so.
  • I think you can backport the class from 1.7/develop without problems, or just switch the entire Auth package to 1.7/develop.

    We will probably release a 1.6.1 in a week or so, in which the fixes of the last few days will be backported.
  • I just updated my Orm and Auth packages, and it looks like the issue with the update is gone now. One other problem I stumbled upon is deleting a user using Auth::delete_user  does not cascade to the user metadata tables. I saw these lines of code 

    'metadata' => array(
    'model_to' => 'Model\\Auth_Metadata',
    'key_from' => 'id',
    'key_to' => 'parent_id',
    'cascade_delete' => true,
    ),

    in the Auth_User model establishing a cascade delete but for some reason it is not work for me, what might be the problem?
  • HarroHarro
    Accepted Answer
    cascade_delete only cascades into relations it knows about (i.e. which are fetched).

    If you go into the delete_user method, and change the query to

    $user = \Model\Auth_User::query()
        ->related('metadata')
        ->select(\Config::get('ormauth.table_columns', array()))
        ->where('username', '=', $username)
        ->get_one();

    is it fixed then?
  • Thank you so much for all the help. The delete is now cascading to the metadata. Thanks again for everything. Keep up the good work!


  • Ok, thanks for the confirmation. I've pushed the fix to 1.7/develop.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion