In yii2 I have a controller and model built with gii tool. However when I call the model function from the controller, it gives an error.
PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'backend\controllers\TargetCities' not found'
in C:\xampp\htdocs\ncddp\backend\controllers\TargetCitiesController.php:15
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}
This is my Controller.........................................
<?php
namespace backend\controllers;
class TargetCitiesController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->render('index');
}
function actionGetcities()
{
$prov_code = $_POST['prov_code'];
$cities = TargetCities::model()->get_cities($prov_code);
echo $cities;
}
}
and this is my model...................
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "tbl_target_cities".
*
* @property integer $id
* @property string $city_code
* @property string $kc_classification
* @property integer $cluster
* @property integer $grouping
* @property string $priority
* @property integer $launch_year
*/
class TargetCities extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'tbl_target_cities';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['city_code', 'kc_classification', 'cluster', 'grouping', 'priority', 'launch_year'], 'required'],
[['cluster', 'grouping', 'launch_year'], 'integer'],
[['city_code'], 'string', 'max' => 20],
[['kc_classification', 'priority'], 'string', 'max' => 100],
[['city_code'], 'unique']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'city_code' => 'City Code',
'kc_classification' => 'Kc Classification',
'cluster' => 'Cluster',
'grouping' => 'Grouping',
'priority' => 'Priority',
'launch_year' => 'Launch Year',
];
}
public function get_cities(){
$select = "SELECT * FROM lib_cities where prov_code=$prov_code";
$query = Yii::app()->db->createCommand($select)->queryAll();
return $query;
}
}
Your lost use app\models\TargetCities
. Because you use namespace in Php. And if you not set use ...
for TargetCities
model Php find it class in 'backend\controllers' namespace, but it not find. Add in controller
like that:
use app\models\TargetCities;
class TargetCitiesController extends \yii\web\Controller
{
Edit.
You use Yii1 style not Yii2. In Yii2 not method model()
. Correct in controller:
function actionGetcities()
{
$prov_code = $_POST['prov_code'];
$cities = TargetCities::get_cities($prov_code);
echo $cities;
}
And in model TargetCities
declare get_cities
to static
and change Yii::app()
to Yii::$app
like that:
public static function get_cities(){
$select = "SELECT * FROM lib_cities where prov_code=$prov_code";
$query = Yii::$app->db->createCommand($select)->queryAll();
return $query;
}
Read doc for defferent Yii2 and Yii2 - http://www.yiiframework.com/doc-2.0/guide-intro-upgrade-from-v1.html
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments