Search code examples
phpcakephpuser-registration

User Registration with cakephp 2?


Im a newbie to cakephp and have some experience of php. Ive used cakephp 1.3 and am using cakephp 2.06 atm.

I have a question which is bugging me;

I want to create a user registration system for 3 types of users:

  • customer
  • Retailer
  • Manufacturer

Given that all 3 users may have different fields whats the best way to do this with cakephp?

  1. create all the fields needed in a users table and use that (with an field for setting the user type) where i can check and ask for/show the needed data.

  2. create 3 tables one for each and have a foreign key linking them. (seems complicated)


Solution

  • You'll at least want to keep all login-info (username/password) in the users table. Assuming each user type has somewhat overlapping data, you're probably fine keeping all the fields in the users table (or a related 'profiles' table).

    If you'd rather separate the data, you could have profile_customers, profile_retailers...etc. But I'd only do that if it's nearly all different data - and I assume most will have 'phone', 'email'...etc

    Easiest way that comes to mind to create a different registration form for each type is just linking to your registration form based on the type of user:

    http://www.mysite.com/users/register/customer
    

    Then, in your controller:

    function register($type = null) {
        //...
        switch($type) {
            case 'customer':
                $this->render('registration_customer');
                break;
            case 'retailer':
                $this->render('registration_retailer');
                break;
            case 'manufacturer':
                $this->render('registration_manufacturer');
                break;
        }
        //...
    

    Then, you can have a separate CTP file for each registration page. Or, if they overlap a lot, you could also just pass a variable from your controller to the view, and use if statements to display only the relevant fields.

    There are so many ways this could be done, but the main answer to your question is - unless they vary drastically in the data you want to store, probably best to keep them in the same table - and for sure at least keep the username/password or email/password in the users table.