I am new in Yii, currently working on a project.
in which there are two tables…
-
kash_users(kash_userID,kash_firstName,etc.)
-
kash_user_login(kash_user_login_id, user_password,user_email,etc…)
I created two models for both tables, and a single controller, the problem is data is inserting into kash_users table but not in kash_user_login table, also email and password validation problem is also there, plz help me…
My models are :
model/Users.php(for kash_users table)
<?php
class Users extends CActiveRecord
{
public $PasswordConfirm;
public function tableName()
{
return 'kash_users';
}
/**
* @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('kash_firstName, kash_lastName, kash_country', 'required'),
array('kash_firstName', 'unique'),
//array('profile', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
);
}
/**
* @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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'kash_userId' => 'Kash User',
'kash_user_login_id' => 'Kash User Login',
'kash_firstName' => 'Kash First Name',
'kash_lastName' => 'Kash Last Name',
//'kash_address' => 'Kash Address',
'kash_country' => 'Kash Country',
//'kash_pincode' => 'Kash Pincode',
//'kash_phone' => 'Kash Phone',
//'kash_user_denomination' => 'Kash User Denomination',
//'kash_createDate' => 'Kash Create Date',
//'kash_updateDate' => 'Kash Update Date',
);
}
/**
* 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->compare('kash_userId',$this->kash_userId);
$criteria->compare('kash_user_login_id',$this->kash_user_login_id);
$criteria->compare('kash_firstName',$this->kash_firstName,true);
$criteria->compare('kash_lastName',$this->kash_lastName,true);
$criteria->compare('kash_address',$this->kash_address,true);
$criteria->compare('kash_country',$this->kash_country);
$criteria->compare('kash_pincode',$this->kash_pincode,true);
$criteria->compare('kash_phone',$this->kash_phone,true);
$criteria->compare('kash_user_denomination',$this->kash_user_denomination,true);
$criteria->compare('kash_createDate',$this->kash_createDate,true);
$criteria->compare('kash_updateDate',$this->kash_updateDate,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* 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 Users the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
mode/UserLogin.php (for kash_user_login table)
<?php
class UserLogin extends CActiveRecord
{
//public $password_repeat;
public $PasswordConfirm;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'kash_user_login';
}
public function validatePassword($user_password)
{
return CPasswordHelper::verifyPassword($user_password,$this->user_password);
}
public function hashPassword($password)
{
return CPasswordHelper::hashPassword($password);
}
/**
* @return string the associated database table name
*/
/**
* @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('user_email, user_password', 'required'),
array('user_email', 'unique'),
//array('user_email','preg_match()')
array('kash_usertype_id', 'numerical', 'integerOnly'=>true),
array('user_email, user_password, kash_user_balance', 'length', 'max'=>255),
array('user_password', 'compare'),
array('PasswordConfirm', 'safe'),
array('kash_userStatus', 'length', 'max'=>1),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('kash_user_login_id, user_email, user_password, kash_usertype_id, kash_user_balance, kash_userStatus, date_created, date_updated', 'safe', 'on'=>'search'),
);
}
/**
* @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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'kash_user_login_id' => 'Kash User Login',
'user_email' => 'User Email',
'user_password' => 'User Password',
'kash_usertype_id' => 'Kash Usertype',
'kash_user_balance' => 'Kash User Balance',
'kash_userStatus' => 'Kash User Status',
'date_created' => 'Date Created',
'date_updated' => 'Date Updated',
);
}
/**
* 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->compare('kash_user_login_id',$this->kash_user_login_id);
$criteria->compare('user_email',$this->user_email,true);
$criteria->compare('user_password',$this->user_password,true);
$criteria->compare('kash_usertype_id',$this->kash_usertype_id);
$criteria->compare('kash_user_balance',$this->kash_user_balance,true);
$criteria->compare('kash_userStatus',$this->kash_userStatus,true);
$criteria->compare('date_created',$this->date_created,true);
$criteria->compare('date_updated',$this->date_updated,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* 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 UserLogin the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
my contrllers file is :
controller/ UsersContrller.php(for Users Model)
<?php
class UsersController extends Controller
{
/*public function actionIndex()
{
$this->render('index');
}
*/
//public $layout='//layouts/column2';
// Uncomment the following methods and override them if needed
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','create'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
public function actionView($kash_userId)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
public function actionCreate()
{
$model1 = new Countrylist;
if(isset($_POST['Countrylist']))
{
$model->attributes=$_POST['Countrylist'];
if($model->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
$model=new Users;
$model_login = new UserLogin;
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model_login);
if(isset($_POST['Users']) && isset($_POST['UserLogin']))
{
$model->attributes=$_POST['Users'];
if($model->save())
{
$this->redirect(array('view','id'=>$model->kash_userId));
$this->redirect(array('view','id' => $model_login->kash_user_login_id));
}
$valid=$model->validate();
$valid=$model_login->validate();
if($valid)
{
// use false parameter to disable validation
$model->save(false);
$model_login->save();
// ...redirect to another page
}
}
/*
if(isset($_POST)){
if(isset($_POST[‘Users’])) {
$model->attributes=$_POST[‘Users’];
$model->save();
}
if(isset($_POST[‘UserLogin’])){
$model_login->attributes=$_POST[‘UserLogin’];
$model_login->save();
}
//Redirect to your target page
//$this->redirect(’/’);
}
*/
$this->render('create',array(
'model'=>$model,
'model_login'=>$model_login,
));
}
}
views\users\_form.php file :
<?php
/* @var $this UsersController */
/* @var $model Users */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'users-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model_login,'Email'); ?>
<?php echo $form->textField($model_login,'user_email',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model_login,'user_email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'First Name'); ?>
<?php echo $form->textField($model,'kash_firstName',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'kash_firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'LastName'); ?>
<?php echo $form->textField($model,'kash_lastName',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'kash_lastName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Country'); ?>
<?php echo $form->dropDownList($model,'kash_country',CHtml::listData(Countrylist::model()->findAll(),'kash_counrtyId', 'kash_countryName'));?>
<?php echo $form->error($model,'kash_country'); ?>
<div class="row">
<?php echo $form->labelEx($model_login,'password'); ?>
<?php echo $form->passwordField($model_login,'user_password',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model_login,'password'); ?>
</div>
<div class="row">
<?php echo $form->label($model_login,‘PasswordConfirm’); ?>
<?php echo $form->passwordField($model_login,‘PasswordConfirm’,array(‘size’=>60,‘maxlength’=>256)); ?>
<?php echo $form->error($model_login,‘PasswordConfirm’); ?>
</div>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Register' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
view\users\create.php file :
<?php
/* @var $this UsersController */
/* @var $model Users */
$this->breadcrumbs=array(
'Users'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List Users', 'url'=>array('index')),
array('label'=>'Manage Users', 'url'=>array('admin')),
);
?>
<h1>Register</h1>
<?php $this->renderPartial(’_form’, array(‘model’=>$model,‘model_login’=>$model_login)); ?>
PLZ help me !! Thank you all