L
L
Lomoson2014-12-12 15:59:02
Yii
Lomoson, 2014-12-12 15:59:02

Updating a model in Yii?

Sorry for the stupid question, but please tell me how to update correctly using one of the validation rules?
If you do this:

$model = new Forms('edit_form');
$model->attributes = $_POST['Forms'];
if ($model->validate())
     $model->save(false);

then a new entry will be created.
If so:
$form = User::model()->findByPk($id);
$form->name = 'name';
$user->update();

then the record will be updated, but the necessary validation rules will not be applied.
Alternatively, you can check through new Forms('edit_form'), and then $form = User::model()->findByPk($id); and then assign valid attributes, but this is somehow stupid.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dotzero, 2014-12-12
@dotzero

if ($model->validate())
     $model->update();

Also, for example, you can make different validation scripts and use one of them for saving, and others for updating.
www.yiiframework.com/doc/api/1.1/CModel#scenario-detail

M
Max, 2014-12-12
@AloneCoder

You need to specify the right rule not only for the edit_form script

D
DjSebas, 2014-12-12
@DjSebas

www.yiiframework.com/doc/api/1.1/CModel#validate-detail
Something like:

$form = User::model()->findByPk($id);
$form->name = 'name';
if ( $form->validate(array('name')) ) {
    $user->update();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question