CrazyCat
(Crazycat)
August 12, 2013, 9:21am
1
Hello there.
I’m trying to validate a form wich has an element not present in the corresponding DB table.
So, I’ve added it in the model (public $myfield;) and in the validating rules.
But whatever I try, when doing $model->save(), I get an error: myfield is empty. And if I dump $model->attributes, I don’t see myfield.
What do I forget ?
u can check the that model class rules and attributes may be its help full for u and next time u post pls ill give clear?
CrazyCat
(Crazycat)
August 12, 2013, 10:27am
4
My trouble is that the attribute I created is not appearing in the $model->attributes array.
So, when validating the form, it’s alway considered as empty.
The validation rules works fine, but this attibute is never seen in the model.
My model looks like:
/**
* @property integer $id;
* @property string $name;
* @property string $email;
*/
class MyModel extends CActiveRecord
{
public $myNewAttr;
public function rules()
{
return array(
array('name, email', 'required', 'on' => 'insert'),
array('email, myNewAttr', 'required', 'on' => 'update'),
array('id, name, email, myNewAttr', 'safe', 'on' => 'search'),
);
}
...
}
And in my controller:
public function actionUpdate($id)
{
$model = $this->loadModel($id);
$model->setAttributes($_POST['User']);
if ($model->save())
{
$this->redirect(array('view', 'id' => $id));
}
}
And when updating, I alway get the error telling that myNewAttr is empty.
I’ve tryed to force it with getter and setter, or explicite attribution ($model->setAttribute(‘myNewAttr’, $_POST[‘User’][‘myNewAttr’]), nothing works.
Ankit_Modi
(Ankit Modi)
August 12, 2013, 10:39am
5
Hi,
try this…
$model->myNewAttr= $_POST['User']['myNewAttr'];
before setAttributes…
otherwise
add a line on rule function…
array('myNewAttr', 'default', 'setOnEmpty' => true, 'value' => null),
CrazyCat
(Crazycat)
August 12, 2013, 10:44am
6
Ok, it was so simple…
I’d tried, but after the setAttributes and it failed.
Thanks a lot.
Ankit_Modi
(Ankit Modi)
August 12, 2013, 10:51am
7
Hi,
cool
can you post the code? so someone help this…
CrazyCat
(Crazycat)
August 12, 2013, 10:58am
8
Yes
The only correction is in the controller:
public function actionUpdate($id)
{
$model = $this->loadModel($id);
if (isset($_POST['User']) {
$model->myNewAttr = $_POST['User']['myNewAttr']; // Forces the myNewAttr attribute
$model->setAttributes($_POST['User']);
if ($model->save())
{
$this->redirect(array('view', 'id' => $id));
}
}
$this->render('update', array(
'model' => $model,
));
}
explorer
(Xdim11)
August 12, 2013, 4:50pm
10
I’m curious, previously you did this:
$model->setAttribute('myNewAttr', $_POST['User']['myNewAttr']
and it didn’t work, and then you change to this:
$model->myNewAttr = $_POST['User']['myNewAttr'];
and it works now, what’s the different between setAttribute and direct assignment? I thought they’re the same.
Looking here http://www.yiiframework.com/doc/api/1.1/CActiveRecord#setAttribute-detail , it says that:
From what being said there, I conclude that setAttribute is the same as direct assignment, but not in this case?
CrazyCat
(Crazycat)
August 12, 2013, 7:34pm
11
I think the difference is not the way it’s done but when it’s done.
I already have this issue, but in another context, and using the setAttribute worked.
Ankit_Modi
(Ankit Modi)
August 13, 2013, 3:34am
12
Hi
$model->setAttribute
means you can set all attributes on DB but the reason is is if you want to add a any field after create the model so you want to write a code and defined the public property on model.
$model->myNewAttr = $_POST[‘User’][‘myNewAttr’];
2)second option is otherwise you can regenerate the model
Attributes are columns from the table. Since they’re not defined in the Active Record class they are read from the database schema when a model is instantiated.
Extra properties defined in the model are not added to the attribute list read from schema. That’s why you can’t set them using setAttributes() method.
explorer
(Xdim11)
August 13, 2013, 2:58pm
14
nineinchnick:
Attributes are columns from the table. Since they’re not defined in the Active Record class they are read from the database schema when a model is instantiated.
Extra properties defined in the model are not added to the attribute list read from schema. That’s why you can’t set them using setAttributes() method.
Thanks for your good and clear explanation. I think my previous post made confusion between setAttribute and setAttributes.
CrazyCat
(Crazycat)
August 13, 2013, 7:27pm
15
Ok, I see. Good explanation, I’ll try to remember it