php - Assigning array data to a model properties in Yii -
i working on method activate new members. following error:
undefined offset: 1
the error occurs on line contains following:
$model->username=$data[1];
is there yii way make work better?
i rookie php, oop, , yii, assistance appreciated greatly!
the method takes request user. runs method , contains key used validate account. validations stored in file same name key. inside file pipe delimited line contains date, username, email, salt, , password hash. after reading file, , exploding data @ pipe, need assign data model, , saved db.
... $model=new users; $model->activation_key=$_get['key']; $path_to_validation_files = yii::app()->basepath."/data/new_member_validations"; $validation_file = $path_to_validation_files . "/".$model->activation_key.".dat"; if(is_readable($validation_file) && !empty($validation_file)) { if($fp = fopen($validation_file, 'r')) { while(!feof($fp)) { $line = fread($fp, filesize($validation_file)); } fclose($fp); $data = explode('|', $line); if(!empty($data)){ $model->username=$data[1]; $model->email=$data[2]; $model->salt=$data[3]; $model->password=$data[4]; } else { throw new chttpexception(500,'validation file contained no data.'); } } else { throw new chttpexception(500,'cannot open validation file.'); } } else { throw new chttpexception(404,'validation file not found, not readable, or empty.'); } ...
you can use attributes property of cactiverecord class load associative array.
for example, have model properties foo , bar, , have array data('foo'=>1, 'bar'=2)
you can set $mymodel->attributes = data, , loop through array , assign properties model (but if attributes have validation rules)
Comments
Post a Comment