Here is the model code
<?php
/**
* This is the model class for table "tbl_user_master".
*
* The followings are the available columns in table 'tbl_user_master':
* @property integer $USER_ID
* @property string $EMAIL
* @property string $PASSWORD
* @property string $USER_TYPE
* @property integer $HOTEL_ID
* @property string $FIRST_NAME
* @property string $LAST_NAME
* @property string $USER_STATUS
* @property double $CREATED_ON
* @property double $UPDATED_ON
*/
class User extends CActiveRecord
{
public $REPEAT_PASSWORD;
public $INIT_PASSWORD;
public $assignee_name;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_user_master';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('EMAIL, USER_TYPE, FIRST_NAME, LAST_NAME, USER_STATUS', 'required'),
array('HOTEL_ID', 'numerical', 'integerOnly'=>true),
array('EMAIL', 'email'),
array('EMAIL', 'unique'),
array('PASSWORD, REPEAT_PASSWORD', 'required', 'on'=>'insert'),
array('PASSWORD, REPEAT_PASSWORD', 'length', 'min'=>6, 'max'=>40),
array('PASSWORD', 'compare', 'compareAttribute'=>'REPEAT_PASSWORD'),
array('CREATED_ON, UPDATED_ON', 'numerical'),
array('HOTEL_ID', 'validhotel'),
array('EMAIL, FIRST_NAME, LAST_NAME', 'length', 'max'=>50),
// array('PASSWORD', 'length', 'max'=>32),
array('USER_TYPE', 'length', 'max'=>5),
array('USER_STATUS', 'length', 'max'=>1),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('USER_ID, EMAIL, PASSWORD, USER_TYPE, HOTEL_ID, FIRST_NAME, LAST_NAME, USER_STATUS', 'safe', 'on'=>'search'),
);
}
public function validhotel($attribute,$params) {
if(!($this->USER_TYPE == 'admin' || $this->USER_TYPE == 'cro') && empty($this->$attribute)) {
$this->addError($attribute, 'Please select Hotel');
}
return true;
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'hotel' => array(self::BELONGS_TO, 'Hotel', 'HOTEL_ID'),
'invoice' => array(self::HAS_MANY, 'Invoice', 'USER_ID'),
);
}
public function defaultScope()
{
$data = array();
if(! Yii::app()->user->isGuest) {
if(Yii::app()->user->user_type == "admin" || Yii::app()->user->user_type == "cro") {
} elseif(Yii::app()->user->user_type == "hotel") { //if user is hotel user, show all user in that hotel
$data = array(
'condition' => $this->getTableAlias(false, false).'.HOTEL_ID=:hotel_id AND ('.$this->getTableAlias(false, false).'.USER_TYPE = (:utype) OR '.$this->getTableAlias(false, false).'.USER_TYPE = (:utype2))',
'params' => array(
':hotel_id' => Yii::app()->user->hotel_id,
':utype' => 'hotel',
':utype2' => 'sale'
)
);
} else { //if user is sale person, show only sale persons account
$data = array(
'condition' => $this->getTableAlias(false, false).'.HOTEL_ID=:hotel_id AND '.$this->getTableAlias(false, false).'.USER_ID=:user_id',
'params' => array(
':hotel_id' => Yii::app()->user->hotel_id,
':user_id' => Yii::app()->user->user_id
)
);
}
}
return $data;
}
public function hotelAdmin($hotel_id = 0)
{
$this->getDbCriteria()->mergeWith(array(
'condition'=>$this->getTableAlias(false, false).'.HOTEL_ID=:hotel_id AND '.$this->getTableAlias(false, false).'.USER_TYPE=:utype',
'params' => array(
':hotel_id' => $hotel_id,
':utype' => 'hotel'
)
));
return $this;
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'USER_ID' => 'User',
'EMAIL' => 'Email',
'PASSWORD' => 'Password',
'USER_TYPE' => 'User Type',
'HOTEL_ID' => 'Hotel',
'FIRST_NAME' => 'First Name',
'LAST_NAME' => 'Last Name',
'USER_STATUS' => 'User Status',
'CREATED_ON' => 'Created On',
'UPDATED_ON' => 'Updated On',
);
}
protected function beforeValidate() {
foreach($this->attributes as $key => $value) {
if(is_string($value)) {
$this->$key = trim($value);
}
}
return true;
}
public function beforeSave()
{
// in this case, we will use the old hashed password.
if(empty($this->PASSWORD) && empty($this->REPEAT_PASSWORD) && !empty($this->INIT_PASSWORD)) {
$this->PASSWORD=$this->REPEAT_PASSWORD=$this->INIT_PASSWORD;
} elseif(!empty($this->PASSWORD) && !empty($this->REPEAT_PASSWORD) && ($this->PASSWORD == $this->REPEAT_PASSWORD)) {
$this->PASSWORD = md5($this->PASSWORD);
$this->REPEAT_PASSWORD = md5($this->REPEAT_PASSWORD);
}
if($this->isNewRecord) {
$this->CREATED_ON = time();
} else {
$this->UPDATED_ON = time();
}
return parent::beforeSave();
}
public function afterFind()
{
//reset the password to null because we don't want the hash to be shown.
$this->INIT_PASSWORD = $this->PASSWORD;
$this->PASSWORD = null;
$this->assignee_name = $this->FIRST_NAME . ' ' . $this->LAST_NAME;
if(!Yii::app()->user->isGuest && (Yii::app()->user->user_type == 'admin' || Yii::app()->user->user_type == 'cro')) {
if(!empty($this->hotel)) {
$this->assignee_name = $this->hotel->HOTEL_NAME . ' - '.$this->FIRST_NAME . ' ' . $this->LAST_NAME;
}
}
parent::afterFind();
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('hotel');
$criteria->compare('USER_ID',$this->USER_ID);
$criteria->compare('EMAIL',$this->EMAIL,true);
$criteria->compare('PASSWORD',$this->PASSWORD,true);
$criteria->compare('USER_TYPE',$this->USER_TYPE,true);
$criteria->compare($this->getTableAlias(false, false).'.HOTEL_ID',$this->HOTEL_ID);
$criteria->compare('FIRST_NAME',$this->FIRST_NAME,true);
$criteria->compare('LAST_NAME',$this->LAST_NAME,true);
$criteria->compare('USER_STATUS',$this->USER_STATUS,true);
// $criteria->compare('CREATED_ON',$this->CREATED_ON);
// $criteria->compare('UPDATED_ON',$this->UPDATED_ON);
$sort=new CSort;
$sort->defaultOrder = 'USER_ID DESC';
$sort->attributes=array(
'*',
'HOTEL_ID' => array( "asc"=>'hotel.HOTEL_NAME', "desc" => 'hotel.HOTEL_NAME desc')
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return User the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
and action :
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
$this->redirect(array('view','id'=>$model->USER_ID));
}
$this->render('update',array(
'model'=>$model,
));
}
And my form :
<div class="row">
<?php echo $form->labelEx($model, 'PASSWORD'); ?>
<?php echo $form->passwordField($model, 'PASSWORD', array('size' => 40, 'maxlength' => 40, 'minlength' => 6)); ?>
<?php echo $form->error($model, 'PASSWORD'); ?>
</div>