简体   繁体   中英

How to call a method from model in YII 2 framework?

I am newbie in YII framework. I have installed correctly and created a Test Controller & Test Model using GII extension of YII. I have created a method in Model and want to access in Controller but unable to access.

Test controller

    <?php

namespace app\controllers\api;

use Yii;
use app\models\api\Test;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

class TestController extends \yii\web\Controller
{
    public $modelClass = 'app\models\api\Test';
    private $model;

    public function filters(){
            return array(
                'accessControl', // perform access control for CRUD operations
                array(
                    'RestfullYii.filters.ERestFilter + 
                    REST.GET, REST.PUT, REST.POST, REST.DELETE, REST.OPTIONS'
                ),
            );
    }
    public function actions(){
            return array(
                'REST.'=>'RestfullYii.actions.ERestActionProvider',
            );
    }
    public function accessRules(){
            return array(
                array('allow', 'actions'=>array('REST.GET', 'REST.PUT', 'REST.POST', 'REST.DELETE', 'REST.OPTIONS'),
                'users'=>array('*'),
                ),
                array('deny',  // deny all users
                    'users'=>array('*'),
                ),
            );
    }


    protected function loadModel( $id = null )
    {
        if($this->model===null)
        {
            if($id!==null)
                $this->model=TestModel::model()->findByPk($id);
        }
        return $this->model;
    }

    public function actionIndex()
    {
       //return $this->render('index');
       //$array = $modelClass::model()->listUserData();
        //$array = Yii::app()->model()->listUserData();
        //$array = $modelClass->listUserData();
       // echo TestModel()->listUserData();
        print "<pre>";print_r($this->model->listUserData());
        exit;
    }

}

Test Model

    <?php

namespace app\models\api;

use Yii;

/**
 * This is the model class for table "users".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $activkey
 * @property integer $createtime
 * @property integer $lastvisit
 * @property integer $superuser
 * @property integer $status
 */
class Test extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'users';
    }

    /** 
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password', 'email'], 'required'],
            [['createtime', 'lastvisit', 'superuser', 'status'], 'integer'],
            [['username'], 'string', 'max' => 50],
            [['password', 'email', 'activkey'], 'string', 'max' => 128],
            [['username'], 'unique'],
            [['email'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'activkey' => 'Activkey',
            'createtime' => 'Createtime',
            'lastvisit' => 'Lastvisit',
            'superuser' => 'Superuser',
            'status' => 'Status',
        ];
    }
     public static function listUserData(){
            $UserData = Test::model()->findAll('status = "0"');
            return $UserData;
        }
}

i tried to search on forum but not able to resolve, please can you help me to resolve ?

Thanks in advance.

Its simple create a instance of Model and then call the required function

like

public function actionIndex()
    {
          $model = new Test();
          print "<pre>";print_r($model->listUserData());
        exit;
    }

Try this

public static function listUserData(){

         $UserData = Test::findAll(['status' =>0]);//in Yii2
         //$UserData = Test::model()->findByAttributes(array( 'status' => 0 )); in yii 1.1

            return $UserData;            
}

In the controller.

use pathtotestmodel/Test

public function actionIndex()
{
$model = new Test();
$userdata = $model->listUserData();
}

OR

public function actionIndex()
{
$userdata = Test::listUserData();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM