0

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 ...

2 Answers2

1

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.

Prabowo Murti
  • 1,216
  • 2
  • 18
  • 27
  • wow ... this work Mr. Prabowo Murti Helper class is collection of function that's I made for specific task, such as generate unique code etc .. – Triwahyu Pamungkas Pribadi Feb 26 '17 at 16:56
  • Great. I suggest you to read about `Yii::$app->db->getLastInsertID();` to get last inserted ID. If you found that my answer helps you, please kindly mark it as the accepted answer. Terima kasih :) – Prabowo Murti Feb 27 '17 at 06:21
0

Yii2 didn't find ModelName class.

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

IStranger
  • 1,868
  • 15
  • 23