BUM - Add Additional Info to a User

You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#1)

How to add additional information to a user when using BUM extension?

1 Create the necessary tables; something like:

CREATE TABLE users_info1 (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    
    additional_data1_info1 VARCHAR(250),
    additional_data2_info1 VARCHAR(250),
    
  PRIMARY KEY (id) ,
  
  FOREIGN KEY (id) REFERENCES users (id )
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=InnoDB;

CREATE TABLE users_info2 (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    
    id_user INT UNSIGNED NOT NULL,
    
    additional_data1_info2 VARCHAR(250),
    additional_data2_info2 VARCHAR(250),
    
  PRIMARY KEY (id) ,
  
  FOREIGN KEY (id_user) REFERENCES users (id )
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=InnoDB;

Table users_info1 is in a relation of one to one to a user and table users_info2, in a relation of one to many (so, for a user there can be more then one field in table users_info2).

2 Create table models in your application (for example in application.models):

models/UsersInfo1.php

<?php

/**
 * This is the model class for table "users_info1".
 *
 * The followings are the available columns in table 'users_info1':
 * @property string $id
 * @property string $additional_data1_info1
 * @property string $additional_data2_info1
 *
 * The followings are the available model relations:
 * @property Users $id0
 */
class UsersInfo1 extends CActiveRecord
{
   // ...
}

models/UsersInfo2.php

<?php

/**
 * This is the model class for table "users_info2".
 *
 * The followings are the available columns in table 'users_info2':
 * @property string $id
 * @property string $id_user
 * @property string $additional_data1_info2
 * @property string $additional_data2_info2
 *
 * The followings are the available model relations:
 * @property Users $idUser
 */
class UsersInfo2 extends CActiveRecord
{
  // ...
}

3 Create corresponding CRUD.

The important thing about table users_info1 is the primary key:

id INT UNSIGNED NOT NULL AUTO_INCREMENT,

that is also a foreign key for table users. id field makes the link between a user an it's associated data.

Same for table users_info2, the important filed is :

id_user INT UNSIGNED NOT NULL,

4 You must take care of the logic behind this tables. (Who should be allowed to create, read, update and delete a record)

For example when creating a new record if you want to automatically link it with the current user, in the create form you may write something like:

<?php echo $form->hiddenField($model, 'id', array('value'=>Yii::app()->user->id)); ?>

for table users_info1 (do not forget to declare id as safe or to manually assign it to the new model), or like

<?php echo $form->hiddenField($model, 'id_user', array('value'=>Yii::app()->user->id)); ?>

for table users_info2.