Search code examples
yii2-basic-app

Getting error "PHP Fatal Error – yii\base\ErrorException", "Class 'app\models\Yii' not found" while executing query in yii2


I have just created on basic example for fetching of data from table using this and it worked well, but now I want to write query using yii query builder

($list= Yii::$app()->db->createCommand('select * from country')->queryAll();) 

but in this case I am getting error for

"PHP Fatal Error – yii\base\ErrorException", "Class 'app\models\Yii' not found".

enter image description here


Solution

  • Your Model is not properly namespace Read PHP Namespace

    Change Your Query Code as

    $list= \Yii::$app()->db->createCommand('select * from country')->queryAll();
    

    Change Your Country Model as

    class Country extends \yii\db\ActiveRecord
    

    {

    public static function getAllCountry()
    {
        $list = \Yii::$app->db->createCommand('select * from country')->queryAll();
        echo "<pre>" . print_r($list);
        die;
    }
    

    }

    and to get list from Your Controller:

    \app\models\Country::getAllCountry();