php - How to insert data to table ,before save create user and use id user for table prime (yii2)? -


i have table student , table user user_id foreign key table student. want save information student , create user , set id user user_id in table student

student models:

    class student extends \yii\db\activerecord {   public $username;   public $password;   public $email;  ......  public function rules()   {     return [       [['name', 'lastname', 'tell', 'mobile', 'codemeli', 'birthday' , 'jensiyat', 'address', 'fathername', 'user_id', 'created'], 'required'],       [['tell', 'mobile', 'codemeli', 'postcode', 'number', 'user_id', 'salary', 'remove'], 'integer'],       ['address', 'string'],       ['pic','file'],       ['username','string'],       ['password','string'],       ['email','string'],       [['username','password'],'required'],       ['email','unique'],       ['email','email'],       [['created', 'salary', 'postcode'], 'safe'],       [['name', 'lastname', 'fathername', 'level'], 'string', 'max' => 50],       [['birthday'], 'string', 'max' => 10],       [['number', 'user_id'], 'unique', 'targetattribute' => ['number', 'user_id'], 'message' => 'the combination of number , user id has been taken.'],       [['user_id'], 'exist', 'skiponerror' => true, 'targetclass' => user::classname(), 'targetattribute' => ['user_id' => 'id']],     ];   }  .........  public function beforesave($insert)   {     if (parent::beforesave($insert)) {       $user = new user;        $user->username = $this->username;       $user->email    = $this->email;       $user->setpassword($this->password);       $user->generateauthkey();       $ret=$user->save();        $this->user_id  = $user->id;        return $ret ? $user : null;     }   } 

actioncreate:

  public function actioncreate()     {         $model = new student();          if ($model->load(yii::$app->request->post())) {             // $model->             $model->pic = uploadedfile::getinstance($model, 'pic');             $username = yii::$app->request->post('username');             $password = yii::$app->request->post('password');             $email = yii::$app->request->post('email');             $model->created = time();             $model->number  = 0;             $model->remove  = 1;             $model->level   = 0;             $model->salary  = 0;              if (!empty($model->pic->extension)) {               $name = time() . '.' . $model->pic->extension;               $model->pic = $name;               if ($model->save()) {                 $model->img->saveas('uploads/' . $name);               }             }              var_dump($model->geterrors());             die();             return $this->redirect(['view', 'id' => $model->id]);         } else {             return $this->render('create', [                 'model' => $model,             ]);         }     } 


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -