Yii 1.1: BUM - Add Additional Info to a User

4 followers

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.

Total 1 comment

#15404 report it
firefly at 2013/11/06 11:12pm
adding new fields for a user

Hi, of course that thare are more ways of doing this, just that every way is better for a certain purpose.

If this is not the way you were thinking jut tell me what it's in your mind and maybe there is a way of implementing it.

Leave a comment

Please to leave your comment.

Write new article