Hi,
i created a update profile page.
i have this in the controller to populate the form and also handle update:
$user = User::model()->findByPk(Yii::app()->user->id);
// Collect user input
if (isset($_POST['User'])) {
$user->attributes = $_POST['User'];
if ($user->save()) {
echo "update successfully";
}
else {
echo "update failed";
}
}
// View
$this->render('user_view', array('user'=>$user,));
however, this doesn’t work. although $user->save is true, the record is not updated in the database. i’ve also check that $_POST[‘User’] is returning the updated data but $user->attributes is not saving them.
why is that so?
nuttynibbles:
Hi,
i created a update profile page.
i have this in the controller to populate the form and also handle update:
$user = User::model()->findByPk(Yii::app()->user->id);
// Collect user input
if (isset($_POST['User'])) {
$user->attributes = $_POST['User'];
if ($user->save()) {
echo "update successfully";
}
else {
echo "update failed";
}
}
// View
$this->render('user_view', array('user'=>$user,));
however, this doesn’t work. although $user->save is true, the record is not updated in the database. i’ve also check that $_POST[‘User’] is returning the updated data but $user->attributes is not saving them.
why is that so?
i can assign manually using $user->name= $_POST[‘User’][‘name’]; but this would be troublesome if there’s a list of data to update
andy_s
(Arekandrei)
February 14, 2011, 5:05am
3
What are validation rules for User?
hi,
the rules are:
return array(array(‘fullName’, ‘required’, ‘on’=>‘profile’),);
actually i notice if $user = User::model()->findByPk(Yii::app()->user->id),
i’m able to populate the fields from database but validation cannot work correctly
if i create $user = new User, then im able to validate but there won’t be data to populate
nuttynibbles:
hi,
the rules are:
return array(array(‘fullName’, ‘required’, ‘on’=>‘profile’),);
actually i notice if $user = User::model()->findByPk(Yii::app()->user->id),
i’m able to populate the fields from database but validation cannot work correctly
if i create $user = new User, then im able to validate but there won’t be data to populate
Hey,
maybe ,let me explain hw am i doing it.
basically i got:
UserController.php
User.php class which extends from CActiveRecord and contain all the variables like public $fullName, public $email, etc
UserLogic.php which extends from User.php that does all the business logic
Only for update record, if i were to do this:
$user = User::model()->findByPk(Yii::app()->user->id);
if (isset($_POST['User'])) {
if ($user->validate()) {
...
}
}
$this->render('profile_view', array('user'=>$user,));
it works.
but if i do this:
$user = UserLogic::model()->findByPk(Yii::app()->user->id);
if (isset($_POST['UserLogic'])) {
if ($user->validate()) {
...
}
}
$this->render('profile_view', array('user'=>$user,));
it doesn’t work correctly. it didn’t checked for required field.
mdomba
(Maurizio Domba Cerin)
February 14, 2011, 7:40am
6
You have not posted the view file… so we cannot see what data you have here… but as you are using the "massive assignment"… you have to declare all the attributes you need as safe so that they are properly assigned…
Check this part of the documentation - http://www.yiiframework.com/doc/guide/1.1/en/form.model#securing-attribute-assignments
Hi mdomba,
i’m able to update those values now. tks
however, i’ve another Q. its regarding rules validation.
previously i mentioned i got:
UserController.php
User.php class which extends from CActiveRecord and contain all the variables like public $fullName, public $email, etc
UserLogic.php which extends from User.php that does all the business logic
currently, if function rules() is placed in User.php and i update record by doing this:
$user = User::model()->findByPk(Yii::app()->user->id);
if (isset($_POST['User'])) {
if ($user->validate()) {
...
}
}
$this->render('profile_view', array('user'=>$user,));
it works.
but if i have function rules() in UserLogic.php which is a child class of User.php and i do this to update record:
$user = UserLogic::model()->findByPk(Yii::app()->user->id);
if (isset($_POST['UserLogic'])) {
if ($user->validate()) {
...
}
}
$this->render('profile_view', array('user'=>$user,));
it doesn’t work correctly. it didn’t checked for required field.
My rules are simple:
public function rules() {
return array(
array('fullName', 'required', 'on'=>'profile'),
);
}
basically i realised that function rules() has gotta to be in the same class which extends from CActiveRecord().
if it is implemented in the child class, which in this case is UserLogic.php, it doesn’t work for updating records. however, it works for new records.
Am i in the right track?
mdomba
(Maurizio Domba Cerin)
February 14, 2011, 9:27am
8
not really sure why adding new records would work but updating not…
the validation should be the same regardless of adding new record or updating old one… could be that you use different scenario?
If you don’t find a solution… can you make a simple test project so that we can test this…
nuttynibbles
(Nuttynibbles1984)
February 14, 2011, 12:29pm
9
mdomba:
not really sure why adding new records would work but updating not…
the validation should be the same regardless of adding new record or updating old one… could be that you use different scenario?
If you don’t find a solution… can you make a simple test project so that we can test this…
Hi mdomba,
I’ve attached a simple test project.
I’ve created 4 files:
data/yiitest.sql
controllers/UserController
models/vo/User.php
models/logic/UserLogic.php
In UserController, i’ve created actionAdd() which add without any problem.
There are 3 others edit functions namely:
actionEdit() - both validation and edit doesn’t work
actionEdit2() - edit works but not validation
actionEdit3() - both edit and validation works
mdomba
(Maurizio Domba Cerin)
February 14, 2011, 1:50pm
10
In the model UserLogic you don’t have the model() static method…
Note the documentation - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#model-detail
And for the validation to work you need to add a rule for the "edit" scenario in UserLogic.
nuttynibbles
(Nuttynibbles1984)
February 15, 2011, 12:51pm
11
hey mdomba,
tks for your help, now im able to do it.
appreciate your effort.!!!
mdomba
(Maurizio Domba Cerin)
February 15, 2011, 12:54pm
12
Glad to help… and this way I learn new thing about Yii, too