Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to add extra user data not in EAV.
  • I really need one extra field for users.  agency_id.

    I started extending Auth_User but then I would also have to extent Auth for Auth::user_create and Auth_user::update.

    I then thought about a user_agencies table to simply cross reference them, but relating that to the Auth_User Model and Model_Agencies model still causes issues.

    EAV is not really a solution since I really need an index on company_id since it will be searched through all the time for the users of a specific company and there can be very big numbers of users.

    I think I am about to simply create my own auth based on OrmAuth (a few small tweaks only really).

    But before I do so I was wondering if anyone had a better suggestion.

    Thanks,
    Mike



  • This has been addressed in 1.8/develop, and will be in 1.7.2 which will be released soon.

    It uses the Orm's from_array() method to load the object, so it automatically adapts to changes in the column definition.
  • I am using 1.8/develop.   I added the field in the users table but then I get errors that it does not recognize agency_id.

    I'll update auth again, but I think it's only a week or two old.

    Do I need to define the model changes somewhere?
  • /fuel/packages/auth

    Michaels-MacBook-Pro:auth mikep$ git pull
    Already up-to-date.

    :(

  • This is the commit I was referring to: https://github.com/fuel/auth/commit/61e3c14e9a5bd325e1f41f46b1e10e99504342b1

    Did you add the agency_id to the model $_properties? Because the ORM uses that array to determine whether or not the fieldname passed is a valid column name.
  • Otherwise do some \Orm\Model::from_array() debugging when you call create_user(), to see what exactly happens...
  • So I extended Auth_User and re-defined _properties in there.
    I was thinking of somehow just adding to the array but without overriding __construct or calling some function I could not figure out how. And overriding __construct seemed like a bad idea.
     
    After Auth::create_user I simply assign agency to the model and do a save.  That works.  And is what I did to begin with.

    But now I have gone full circle that when I try Auth::get('agency_id'); it is not found.   All others like email, last_login are found no issue.

  • Of course I can simply do a Auth::user_id or whatever it is and the use Orm to get the whole model.

    I just figured Auth::get should work.
  • get('agency_id) returns $this->user->{$field}, so it uses the standard __get() magic method of the ORM model object to fetch the property.

    You can use the _init() static method to update the existing data structures on first load. I use that in every model, since our base model defines stuff like default columns in every table, default observers, etc.

    Are you sure the Auth package picked up your extended model?
  • I am thinking it does not.  I'll setup a couple of breakpoints and make sure.
    I only have it defined in "fuel/app/bootstap.php'.  Would I need to add it somewhere else for Auth?


  • I think you led me to something...    I am thinking  I need to change 

    'Auth\\Model\\Auth_User'             => __DIR__.'/classes/model/auth/user.php',

    in /packages/auth/bootstrap.php to point to my Auth_User    

    I will give that a try later (switched to another project for now).

    And can you override that in app/bootstrap.php.   Is that the way to go?

    Right now I only have   'Auth_User'     => 
    APPPATH . 'classes/model/user.php',

    I am thinking I need "
    'Auth\\Model\\Auth_User'             =>   APPPATH . 'classes/model/user.php',


  • Ok Changing it in /fuel/packages/auth/bootstrap.php is surely not the way to go because then my class has no way of knowing what it extended.  :)

    Changing it in fuel/app/bootstrap.php gives no errors, but Auth::get still does not work.

    I am not sure what the _init() method is.  
    I am guessing that is a method that is automatically called during construct?  So that is where I can add to the properties array?  (and relateds etc.)
  • I just tested out _init()

    That is exactly what I needed to extend. Thank you so much.

    Now if I can just figure out how to get Auth to use my Auth_User.  :)
       
  • So the first execution point after Auth::get() is


    /fuel/packages/auth/classe/auth.php
    Line: 334 __callStatic


    In there Auth\Auth is already populated with _instance.


    _instance has ‘user’ that is Auth\Model\Auth_User.Not my extended one since it is missing the agency_id property.


    Can this have anything to do with me listing auth as a always_load package? Just guessing here. 


  • All classes in Auth are aliased to global, so you can extend them, like the classes in core.

    So you need

    // in app/classes/model/user/auth.php
    namespace Model;
    class Auth_User extends \Auth\Model\Auth_User {}

    and in your bootstrap map:

    'Model\Auth_User' => APPPATH.'classes/model/user/auth.php',

    and it should work.
  • OK I was not using the Model Namespace.   At my other gig today, but will give it a try tonight.

    Thanks again Harro.



  • OK.  So things are kid of happening.  My user model was missing profile_fields.  No problems there to add that.

    But odd things....

    I HAVE to add protected static $_table_name = 'users'; otherwise some SQL calls have a "FROM AS" (missing the table name).

    I am also having issues with "group" and "group_id".   I will track those down.   
    But which is it suppose to be please?

    Thanks,
    Mike



    This one just for others.... (small typo above)

    bootstrap should be:
    'Model\Auth_User' => APPPATH.'classes/model/auth/user.php',
  • Ormauth doesn't use profile_fields (in the table), it uses EAV for that. But the model defines it for compatibility reasons.
  • Just in case anyone ever reads this thread.

    As mentioned above I am adding protected properties and relationships in my extended class using the _init() function.

    I was not calling parent::_init() and that was the source of a lot of the weirdness I was getting.


  • "Ormauth doesn't use profile_fields (in the table), it uses EAV for that. But the model defines it for compatibility reasons." - Harro Verton

    That explains a bit of how to use EAV and I never thought of checking out the _init() so this is a huge help! Thank you!

Howdy, Stranger!

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

In this Discussion