PHP致命错误' yii \ base \ ErrorException' 消息' Class' Model Name' 未找到

I trying migration my project from yii1 to yii2. I have some model function I make when I still use Yii1, is among others is generate id uniq function's, like this:

public static function generateID($tableName, $modelName) {
        $dateNow = date("Ymd");
        $checkLastID = $modelName::findBySql(
            "SELECT SUBSTR(MAX(id),-4) AS id FROM $tableName WHERE id LIKE '%$dateNow%'"
        )->one();
        $lastNumber = (int)substr($checkLastID["id"], 8,4);

        if($checkLastID["id"] == '') {
            $id = $dateNow.sprintf("%04s", 1);          
        } else {
            $lastNumber = $checkLastID["id"];
            $lastNumber++;
            if($lastNumber < 10) $id = $dateNow.sprintf("%04s", $lastNumber);
            elseif($lastNumber < 100) $id = $dateNow.sprintf("%04s", $lastNumber);
            elseif($lastNumber < 1000) $id = $dateNow.sprintf("%04s", $lastNumber);
            elseif($lastNumber < 10000) $id = $dateNow.sprintf("%04s", $lastNumber);
            else $id = $lastNumber;
        }
        return $id;
    }

and I access the function from controller like this:

$model->id = Helper::generateID('table_name', 'ModelName');

and than, show error when I want create data:

<pre>PHP Fatal Error &#039;yii\base\ErrorException&#039; with message &#039;Class &#039;ModelName&#039; not found&#039; 

in C:\xampp\htdocs\kampunginggrispare.com\common\models\Helper.php:61

Stack trace:
#0 [internal function]: yii\base\ErrorHandler-&gt;handleFatalError()
#1 {main}</pre>

But, If I change

$checkLastID = $modelName::findBySql("SELECT SUBSTR(MAX(id),-4) AS id FROM $tableName WHERE id LIKE '%$dateNow%'")->one();

to be:

$checkLastID = ModelName::findBySql("SELECT SUBSTR(MAX(id),-4) AS id FROM table_name WHERE id LIKE '%$dateNow%'")->one();

It's work, but doesn't work when I use Parameter like function above

In Yii1, not error, but error in Yii2

Any body can help me ??

Thanks ...

Yii2 didn't find ModelName class.

Please read more about upgrading: Upgrading from Version 1.1: Namespace and Yii2 autoloaders.

Try to load $ModelName before use it in the Helper class. You can either

...

$dateNow = date("Ymd");
$className = '\common\models\\' . $modelName; // replace this with your model's namespace
$checkLastID = $className::findBySql(
    "SELECT SUBSTR(MAX(id),-4) AS id FROM $tableName WHERE id LIKE '%$dateNow%'"
)->one();

....

Or simply put your helper in the same namespace with the $ModelName (less recommended). I still don't understand the purpose of your Helper class.