sheraz.s
(Sheraz S)
June 20, 2013, 5:30am
1
Hi, i am trying to save data in table. A model "User" will save user data. I am calling it from another controller named "adminController".
public function actionRegisterAdmin(){
$post_data['admin']['first_name'] = "first";
$post_data['admin']['last_name'] = 'last_name';
$post_data['admin']['email'] = 'email';
$post_data['admin']['password'] = '12345';
$post_data['admin']['gender'] = 1;
$post_data['admin']['country'] = 'country';
$post_data['admin']['state'] = 'state';
$post_data['admin']['acc_type'] = 0;
$user = new User();
$user->attributes = $post_data['admin'];
if($user->validate()){
echo 'IF';
$user->save();
}
else{
"ERROR : ";
$user->getError();
}
}
But neither data is saved nor any exception/error is generated. please guide me how i can get rid of this.
Where/how are you using the controller?
You should call the controller in order to execute that action.
sheraz.s:
Hi, i am trying to save data in table. A model "User" will save user data. I am calling it from another controller named "adminController".
public function actionRegisterAdmin(){
$post_data['admin']['first_name'] = "first";
$post_data['admin']['last_name'] = 'last_name';
$post_data['admin']['email'] = 'email';
$post_data['admin']['password'] = '12345';
$post_data['admin']['gender'] = 1;
$post_data['admin']['country'] = 'country';
$post_data['admin']['state'] = 'state';
$post_data['admin']['acc_type'] = 0;
$user = new User();
$user->attributes = $post_data['admin'];
if($user->validate()){
echo 'IF';
$user->save();
}
else{
"ERROR : ";
$user->getError();
}
}
But neither data is saved nor any exception/error is generated. please guide me how i can get rid of this.
why making complicated??!!
$user = new User();
//$user->attributes = $post_data['admin']; remove this line
$user->first_name = "first";
$user->last_name= "last";
$user->email= "email";
.
.
.
.
.
.
//all here
if($user->validate()){
echo 'IF'; exit;
$user->save();
}
else{
"ERROR : ";
var_dumb($user->getError()); exit;
}
}
check your user model validation rules…and troubleshoot it!!
sheraz.s
(Sheraz S)
June 20, 2013, 6:05am
5
why making complicated??!!
$user = new User();
//$user->attributes = $post_data['admin']; remove this line
$user->first_name = "first";
$user->last_name= "last";
$user->email= "email";
.
.
.
.
.
.
//all here
if($user->validate()){
echo 'IF'; exit;
$user->save();
}
else{
"ERROR : ";
var_dumb($user->getError()); exit;
}
}
var_dumb() is generating error that
Fatal error: Call to undefined function var_dumb() in protected\controllers\AdminController.php on line 119
sheraz.s
(Sheraz S)
June 20, 2013, 6:13am
6
I think i have found the issue. it is about validation rule.I have applied a validation rule to compare passwords. i am not sending password and confirm password but only password.
if i send both values then data is saved otherwise not. i have just use $user->save() method without validation but it still does not save data.
i think validation is automatically called when save() method is called. is there any way to bypass validation.
Hi
U can try the below code
$user = new User();
$user->attributes = $_POST['admin'];
$user->save(false);
if (!$model->hasErrors()) {
Yii::app()->user->setFlash("success", "Thanks for Vendor Regisration.!");
$this->redirect(array('vendor', 'model' => $model));
Yii::app()->end();
}
if you display the flash messages u can put the below code in your view file
<?php
foreach (Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>
hope it will be helpful
sorry var_dump
that means its not validating!!
sheraz.s:
I think i have found the issue. it is about validation rule.I have applied a validation rule to compare passwords. i am not sending password and confirm password but only password.
if i send both values then data is saved otherwise not. i have just use $user->save() method without validation but it still does not save data.
i think validation is automatically called when save() method is called. is there any way to bypass validation.
yes you are correct the save() method call validate() method . see CActiveRecord.php
sheraz.s
(Sheraz S)
June 20, 2013, 7:24am
10
Thanks all you you for you help. i have just assign data and call method $user->save(false);
The issue is resolved now.