dongyou7292 2016-09-08 08:50
浏览 139

如何从YII 2框架中的模型调用方法?

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.

  • 写回答

2条回答 默认 最新

  • doufu6196 2016-09-08 08:53
    关注

    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;            
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c