4

I am working on a method to activate new members. I get the following error:

Undefined offset: 1

The error occurs on the line which contains the following:

$model->username=$data[1];

Is there a Yii way to make this work better?

I am a rookie with php, oop, and Yii, so any assistance would be appreciated greatly!

The method basically takes a get request from a user. The get runs this method and contains a key which is used to validate their account. Validations are stored in a file with the same name as the key. Inside the file is a pipe delimited line that contains the date, username, email, salt, and a password hash. After reading the file, and exploding the data at the pipe, I need to assign the data to the model, and then it will be saved to the 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.');
        }


...
5
  • Are you sure that you are actually getting the line from the file in $line? You can check that with if (empty($line)) die('not getting line') after fclose($fp); .. This is just to make sure, as there seems to be a problem with $line. Also the while loop is redundant as fread will read as many bytes as you have specified i.e. the whole file. Commented Jul 2, 2011 at 19:11
  • what's the output of print_r($data)? Are you sure there is always something in the second array position? Commented Jul 2, 2011 at 19:40
  • @ldg - print_r is Array ( [0] => ) which explains the error, but why is it blank is the other issue :) Commented Jul 2, 2011 at 20:12
  • @Abhijit - I took out the while loop and it now works, at least that part is working ...now it won't save the model ...hopefully I can figure that out. Do you need do something before model->save()? Commented Jul 2, 2011 at 20:14
  • @Abhijit - I did a model->save(false) so it wouldn't try and validate and now it saves. On to the next step :) Commented Jul 2, 2011 at 20:26

2 Answers 2

4

You can use the attributes property of the CActiveRecord class to load an associative array.

For example, say you have a model with properties foo and bar, and you have an array data('foo'=>1, 'bar'=2)

you can set $myModel->attributes = data, and it will loop through the array and assign the properties to the model (but only if those attributes have validation rules)

Sign up to request clarification or add additional context in comments.

3 Comments

+1, thanks. Wish you could just do $model = new MyModel($dataArray);
I believe this doesn't seem to work if, instead of an array, you have an array of arrays though.
I was just wondering how Yii performs this! specifically in Yii2, $model->attributes = $_POST['Formname'] would fill the corresponding model attributes with data? Do you know what goes on behind the scene? I mean how does all data in array gets loaded into the corresponding attribute?
0

I had a similar problem especially when using an array of array. This is how i solved it.

public function epidemicSelectInsertParticipant($class){
    $data=  Participant::findAll(['class'=>$class]);
    if($data){
        foreach ($data as $value) {
            $model = new Epidemicparticipant;
            foreach ($value as $key => $val) {
                if($key=='id'){

                }else{
                        $model->$key = $val;
                }
            }
           $model->save(false);
        }
        return TRUE;
    }  else {
      return false;  
    }
}

In my case,$data is a multi-dimensional array.They keys($key)of $value are the same as $model->attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.