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

1
  • Still error Mr. RiggsFolly Commented Feb 24, 2017 at 14:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 ..
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 :)
0

Yii2 didn't find ModelName class.

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

1 Comment

Okay, I try first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.