Search code examples
phpyii2

How to use deleteAll() function inside a loop in yii2


i am trying to truncate a selected models from my application using deleteAll()

my controller:

there are a lot of models but i didn't include them for maintain as short.

<?php

namespace app\controllers;

use yii\web\Controller;
use app\models\Marks;
use app\models\ActivityHistory;
use app\models\Attendance;
use app\models\Students;
public function actionTruncate(){

    $models = array("Marks","ActivityHistory","Attendance","Students");

    foreach($models as $key=>$model){
        $model::deleteAll();
    }
   exit;
}

this getting error that

Class 'Marks' not found

when i run individual query like Marks::deleteAll(); it works. how to solve this in loop? and suggest me a good way to do this functionality


Solution

  • $models = [Marks::class, ActivityHistory::class, Attendance::class, Students::class];
    
    foreach ($models as $model) {
         $model::deleteAll();
    }