0

I have two tables. one is user-table -name - user and another table named profile.

The format of csv file for user is like this:

first_name,last_name,username,email,phone,user_role,status,password_hash,auth_key,created_at,updated_at,key_date,reset_key,access_token
Abx,xyz ,8917111039,[email protected],891445544,employee,10,$2y$13$jbsLKjG6YtiA1RbilUOeauAS2Zvq5fHB5xh6JnHpfVytvI56IiO32,,,,,,
Alek,buhu,7978019779,[email protected],7978444449,employee,10,$2y$13$jbsLKjG6YtiA1RbilUOeauAS2Zvq5fHB5xh6JnHpfVytvI56IiO32,,,,,,

and there is separate csv file for profile.

for creating new user and profile, I am using a single file and posting the relevant values separately to user and profile table. Now as the number of users are upwards of 1000, I am looking at importing the users in batch mode using csv file.

Another option is to dump the data directly in first user-table and then exporting and getting the user_id and then dumping it in profile table. As I am using Mongodb with auto id field, I cannot make id field as 1,2,3,4

Another advantage I think that I won't be needed to create just one default password, instead I can create some pattern for password.

my controller code to create both profile and user is like below:

public function actionCreate()
    {
        $model = new Employee();
        $user = new AddUser();

        if ($model->load(Yii::$app->request->post()) && $user->load(Yii::$app->request->post()) ) {
           $user->password = $user->password;
           $user->phone = $model->official_contact;
           $user->email = $model->email;
           $user->user_role='employee';

            if ($user = $user->adduser()) {


                $user->auth_key = Yii::$app->security->generateRandomString();
                $user->reset_key = md5(time());
                $user->key_date = date('Y-m-d h:i:s');            
                $user->password_hash = Yii::$app->security->generatePasswordHash($user->password);

            $user->username = $model->email;           

            if($user->save()){

            $model->user_id = (string)$user->_id;
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->file != '') {
                $model->employee_image = $model->file->basename . '.' . $model->file->extension;                 
            }
            $model->save();
            $dir = 'web/img/employee';  
            if ($model->employee_image != '' ) {

                if (!file_exists($dir)) {
                    mkdir($dir);
                }      

            $model->file->saveAs($dir . '/' . $model->file);
            Yii::$app->getTable->imgResize($dir, $model->employee_image, 200, 0, 1, 80);
                //Yii::$app->getTable->imgResize($dir, $model->image_thumb, 350, 0, 1, 80);
                $webpImage=Yii::$app->basePath.'/web/img/employee/'.$model->employee_image;              
                 imagewebp(imagecreatefromjpeg($webpImage), $dir.'/'.basename($model->employee_image,'.jpg').'.webp'); 
            }
            return $this->redirect(['view', 'id' => (string)$model->_id]);
        }
    }
}
        return $this->render('create', [
            'model' => $model,
            'user'=> $user,
        ]);
    }
2
  • if the number of users are going to increase i would strongly advise you NOT to use ActiveRecord as it would be a resource killer and will crash you should instead go for the Yii::$app->db->queryBuilder->batchInsert() to generate the $sql statement and then use the Yii::$app->db-->createdCommand($sql)->execute() to insert the records. Remember none of the events are triggered when using the batchinserts so you might have to take care of that part yourself if you have any nin the User model Commented Apr 30, 2020 at 15:28
  • for generating passwords you can simply use the provided Yii::$app->security->generatePasswordHash('pass12345'); and email the user with the password and instructions to update it, or you can use the Yii::$app->security->generateRandomString() an use it inside the password hash function instead of the pass12345 Commented Apr 30, 2020 at 15:31

0

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.