I want to update a few fields in a table. If I don’t pass in every single field the table does not get updated. I can write a custom sql query to do it, but I’d prefer to use AR. Is there a way to update only a few fields instead of all of them?
Below is my code:
$licensee = Licensee::model()->findByPk($session['licenseeId']);
$licensee->background = $this->background;
$licensee->font = $this->font;
$licensee->width = $this->width;
$licensee->save();
The additional fields in the table are: licenseeId (as PK), signupDate, and domain.
tri
(tri - Tommy Riboe)
June 25, 2011, 5:18pm
2
Save selected attributes with validation call (param 1=true)
$licensee->save(true, array('background', 'font', 'width'));
(untested)
/Tommy
tri:
Save selected attributes with validation call (param 1=true)
$licensee->save(true, array('background', 'font', 'width'));
(untested)
/Tommy
That did not work. I had to add the following rules to be able to pass the field values:
public function rules()
{
return array(
array('topic', 'required'),
array('background', 'safe'),
array('font', 'safe'),
array('width', 'safe'),
);
}
And now when I submit I get the following error:
Licensee has an invalid validation rule. The rule must specify attributes to be validated and the validator name.
tri
(tri - Tommy Riboe)
June 25, 2011, 5:31pm
4
That did not work. I had to add the following rules to be able to pass the field values:
public function rules()
{
return array(
array('topic', 'required'),
array('background', 'safe'),
array('font', 'safe'),
array('width', 'safe'),
);
}
And now when I submit I get the following error:
Licensee has an invalid validation rule. The rule must specify attributes to be validated and the validator name.
That’s strange. You should not need safe validators if you assign values one by one.
Perhaps you can save without validation
$licensee->save(false, array('background', 'font', 'width'));
// or
$licensee->update(array('background', 'font', 'width'));
/Tommy
tri:
That’s strange. You should not need safe validators if you assign values one by one.
Perhaps you can save without validation
$licensee->save(false, array('background', 'font', 'width'));
// or
$licensee->update(array('background', 'font', 'width'));
/Tommy
Tommy - thanks, using “update” worked and I learned something new