Save Multiple Record With Transaction

hi i want to save school model but in school controller when model is saving before saving i need transaction so and in transaction i m inserting others records in database if anything goes wrong then it will rollback and school also will not save and give a error,so my all models depends on one model-school

below is my code

public function actionCreate()


$model = new school();

if ($model->load(Yii::$app->request->post())) {

         $connection = \Yii::$app->db;

         $transaction = $connection->beginTransaction();


        $model->createclass();            //calling class function in school model

        $model->createsection();         // caling create section data in school model



          return $this->redirect(['view', 'id' => $model->name]);

       }catch(ErrorException $e) {


         echo $e->getMessage(); exit;



     return $this->render('_form', [

            'model' => $model,





is it fine

now in school model

 public function createclass(){

    $connection = \Yii::$app->db;

    $connection->createCommand()->batchInsert('class', ['name', 'shortname'], [

['1st', 'i'],

['2nd', '2'],

['3rd', '3'],

['4th', '4'],



    ['12', '12'],



 public function createsection(){

    $connection = \Yii::$app->db;

    $connection->createCommand()->batchInsert('section', ['class_name', 'section'], [

['1st', 'a'],

['1st', 'b'],

 ['1st', 'c'],

  ['1st', 'd'],



           ['12', 'a'],

           ['12', 'b'],

            ['12', 'c'],

             ['12', 'd'],



this is example just

i want to save more then 1000 rows same as i saving above using array and hard coded,but when school is saving it will start transaction and before create school it will save all other models or if anything goes wrong it will rollback and it will create school also and give a error on creating form of school

my question is

where i write transaction in school controller create function or in school model

plz help me