views:

156

answers:

1

I am building a CakePHP app that requires admins, users, and merchants. Merchants will need to have a profile. Users will need to provide different info for their profile.

I currently have a user model handling login with ACL enabled.

That is all working fine.

What I cannot wrap my head around is how do I handle having a different profile for merchants and a different profile for users?

A typical website scenario would just be "user hasOne profile", but I need different profiles based on group type.

I can't seem to grasp how to have different data sets associated with users based on their group.

+1  A: 

If your Users/Merchants/Admins all share the same User model, a polymorphic belongsTo relationship to different Profile models should be the right thing to do. I.e. you make three different profile models like UserProfile, MerchantProfile and AdminProfile. In your User model, you have two fields: profile_class and profile_foreign_key and record data like profile_class: Merchant, profile_foreign_key: 42.

Also see this question.

Alternatively, you just associate the User model with all three profile models in a normal hasOne relationship (where the foreign key goes into the Profile models), but that may make things more complicated.

deceze
Would it make more sense to have models for the Users/Merchants/Admins? If so what would be the best way to handle authentication?
Daniel N
That totally depends on how different your models are. If they're identical except for the single user/merchant/admin attribute, it makes sense to keep them in one model. If you have separate models, you can dynamically configure the AuthComponent in the `beforeFilter` of your user/merchant/admin controllers to use a different model for authentication. You have to decide what makes the most sense/causes the least amount of work.
deceze