A
A
Ainur1232018-05-16 19:05:25
Yii
Ainur123, 2018-05-16 19:05:25

How to create entries in the database in Yii2 after registration?

How in YII2, after the user has registered, create 10 records in another table?
here is the user registration script

<?php
/**
 * Created by PhpStorm.
 * User: User
 * Date: 23.04.2018
 * Time: 19:31
 */

namespace app\models;


use Yii;
use yii\base\Model;

class Signup extends Model
{public $username;
 public $email;
 public $password;
 public function rules()
 {
     return[
       [['email','username','password'],'required'],
         ['email', 'email'],
         ['password','string','min'=>2,'max'=>30]
     ];
 }
 public function signup()
 {
     $developer=new Developers();
     $developer->username=$this->username;
     $developer->email=$this->email;
     $developer->password=Yii::$app->getSecurity()->generatePasswordHash($this->password);
     return $developer->save();


 }
}

here are the fields for 2 tables
$model = new Product;
        $model->category_id='2';
        $model->author_id=$app->user->identity['id'];
        $model->name='name';
        $model->content='content';
        $model->price='0';
        $model->keywords='NULL';
        $model->description='NULL';
        $model->hit='0';
        $model->new='0';
        $model->save();

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
dkrylov, 2018-05-16
@dkrylov

Dig towards events .

D
Denis, 2018-05-17
@sidni

If events are complex, although this is a more correct solution, you can override the afterSave method in the Developers model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question