Nope this is not normal and I am wondering how your code would work. You are accessing a private property!?
rendering your view in the else clause does not fetch the case where validation fails and save() returns false. You should render view even on post request so errors show up in the form when validation is not successfull.
there’s no private properties when __set is used like in its parents. anyway, i’m running code on IIS Win8 x64 + PHP 5.5.7 x64 NTS, maybe it has some impact.
CeBe, are you able to save array of attributes using technique from tutorial?
i didn’t define any. Fine then, i defined rule for model Notes next way
class Notes extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function scopes()
{
return [];
}
public function rules()
{
return [['title', 'length', 'min' => 4]];
}
}
And yes, now it sets $note->attributes and on save puts error " 0 => string ‘Title is too short (minimum is 4 characters).’" which is fine.
So, this is normal behavior to silently refuse setting atributes if i don’t want to apply any validation rules? If i just want to make a bulk save, what can you suggest? (i expect it makes mysql prepared statements which must fix all sort of injections by default)
Btw: it fails silently because it filters out what comes in post. If someone sends you additional fields or for example the submit button also has a value in POST data you do not want that in the record and you still want your record to save instead of failing when the rest of attributes are valid.
public function rules()
{
return [['title']]; //FAIL
return [['title', '']]; //FAIL
}
About fields, i’m not sure what you mean. This class is supposed to get attributes from table schema (which it does, as i cannot assign non-existing attribute for $note)
this is still question for me for saving new record.
another scenario in which i may want to use new record for selection into data provider + grid view
public function actionGetNotes()
{
$dp_notes = new CActiveDataProvider('Notes');
$filter_note = new Notes();
if (!empty($_GET['Notes'])) {
$filter_note->attributes = $_GET['Notes']; //silently FAILS to set values for attributes w/o rules() in Notes model
$dp_notes->setCriteria((new CDbCriteria())
->compare('note_id', $filter_note->note_id, false)
->compare('title', $filter_note->title, true));
}
$this->render('list', [
'dp_notes' => $dp_notes,
'filter_note' => $filter_note
]);
}
which in fact, ignores all rules, as i can serach for title with only 1 letter, but how can i search for created_date if i don’t need rules for its value as it creates on db server? I got a reply live chat that i can select record, reset values and then fill as it is expected with $note->setAttributes(). But why i have to load db server with 1 useless select?