Accessing data from the model class in yii2

beginner

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;
    }
}
vitalik_74

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to get data from model A in view B Yii2

Applying models to data - Inheriting or accessing the attributes of a root class (data) from a sub-class (model)

Accessing data from class selenium

Accessing model methods from controller class Laravel

How to process data in the class behavior public properties in model? (Yii2)

Yii2: Can I attach events from outside the model class?

yii2:data of related model in Gridview

Data not saving through model in yii2

Accessing data from model in Ruby on rails app

yii2 Getting array of data from one model and listing it in view of another

Pass calculated data in a texbox not from any model in dynamic form yii2

Yii2 Grid view syntax to access data from related model

Error in accessing post json data in yii2

Accessing data from a nested class elegantly

How to export model data and relation model to json with Yii2?

accessing Ember data store from a controller / multiple data on model hook

Accessing object from array within an array (defined in model class)

Accessing model data in django

Yii2: How to add validation rules to the model class dynamically?

Yii2 - Wrong User model class being used

Accessing child class data from parent class with PHP OOP

Yii2:how to use model's data in layouts

Yii2 active record model not saving data

Yii2 cannot save model data in controller

Yii2 access class from within the class

When accessing data (DateTimeField) from the django model, it always return None

CakePHP 3 - Model - Accessing data from third layer of array

Laravel accessing relationships - check if data exists from within the model

yii2 hide model name from url in GET Method