On Apr 13, 2:44 am, iqbal-san <[email protected]> wrote:
> Hello,
>
> I'm wondering how best to define the models for users with roles,
> where I want to save a different set of data for each role.  Plus,
> there will be relationships between the roles.  For example, there is
> user type student and user type teacher, where a teacher have many
> students.  Both type of users have some similar methods and attributes
> (e.g. can log into the system, both attend school during the same
> academic year), but each also have its own unique attributes and
> functions.
>
> Currently I'm trying to set teacher and students as an extension of
> the User model, but I found some hassle in bringing up each role's
> data, for example after login.
>
> Should they be totally separate models? but then how to handle the
> login?
>
> I didn't find enough info by googling, but perhaps i entered the wrong
> term?  If this have been discussed in this group before, please point
> me in the right direction.

I've run into this problem several times now and have tried a few
different solutions. The latest is to include model & association_key
columns in the users table. I then also include a $user_id in the
associated tables. This is actually working quite well. One thing I
really didn't like doing was creating the user_id column as allowing
NULL. It was either that, or setting the users table columns as NULL.
Chicken & egg issue when first creating a user.

So, here's a few notes about what I did, adjusted for your scenario.

Usermodel:

var $hasOne = array(
        'Student' => array(
                'className'  => 'Student',
                'foreignKey' => 'user_id',
                'dependent' => true
        ),
        'Teacher' => array(
                'className'  => 'Teacher',
                'foreignKey' => 'user_id',
                'dependent' => true
        ),
        'Administrator' => array(
                'className'  => 'Administrator',
                'foreignKey' => 'user_id',
                'dependent' => true
        )
);

Of course, you'll end up with empty arrays for the model the User
isn't. But I never run a find on User anyway.

var $actsAs = array(
        'Sluggable' => array(
                'translation' => 'utf-8',
                'separator' => '-',
                'label' => array('first_name', 'last_name'),
                'length' => 64,
                'overwrite' => true
        ),
        'AutoField' => array(
                'full_name' => array(
                        'fields' => array('first_name', 'last_name'),
                        'mask' => '%s %s'
                )
        )
);


Student:

var $belongsTo = array(
        'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id'
        )
);

This could go in AppModel except that each User type may have
different things I need contained. Also there are other models, not
associated with User, that I want to find by slug.

public function findBySlug($slug)
{
        return $this->find(
                'first',
                array(
                        'fields' => array('*'),
                        'conditions' => array('User.slug' => $slug),
                        'contain' => array(
                                'User' => array(
                                        'fields' => array(
                                                'User.email',
                                                'User.first_name',
                                                'User.last_name',
                                                'User.slug'
                                        )
                                )
                        )
                )
        );
}

Note that you can have User model in the conditions, just like a table
in a left join.


I create each type in UsersController:

/**
 * Add a new User.
 * A new Administrator, Member, or Affiliate will also be created.
 *
 * @param       void
 * @returns     void
 */
function admin_add($model = null)
{
        if (!empty($this->data))
        {
                $model = $this->data['User']['model'];

                if ($this->User->{$model}->save($this->data))
                {
                        $association_key = 
$this->User->{$model}->getLastInsertID();
                        $this->data['User']['association_key'] = 
$association_key;

                        $password = $this->Password->create();
                        $this->data['User']['password'] = $password['hash'];

                        $this->User->create();

                        if ($this->User->save($this->data))
                        {
                                $user_id = $this->User->getInsertID();

                                $this->User->{$model}->saveField('user_id', 
$user_id);

                                /* queue up an email
                                 */
                                $this->Queue->load(
                                        'swiftmailer',
                                        array(
                                                'settings' => array(
                                                        'view' => 'new_' . 
strtolower($model),
                                                        'subject' => 'AFVBM',
                                                        'from' => 
array(Configure::read('email_no_reply'),
Configure::read('email_name')),
                                                        'to' => 
$this->data['User']['email']
                                                ),
                                                'vars' => array(
                                                        'password' => 
$password['cleartext'],
                                                        'first_name' => 
$this->data[$model]['first_name'],
                                                        'last_name' => 
$this->data[$model]['last_name']
                                                )
                                        )
                                );

                                $this->flash(
                                        'The User has been saved',
                                        array('controller' => 'users', 'action' 
=> 'index', 'admin' => 1)
                                );
                        }
                        else
                        {
                                $this->flash('The User could not be saved. 
Please, try again.');
                        }
                }
        }
        $this->set('model', $model);
        $this->set('regions', $this->User->Member->Region->find('list'));
        $this->set('countries', $this->User->Member->Country->find('list'));
}

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to