私はYIIフレームワークの初心者です。私は正しくインストールし、YIIのGII拡張を使用してテストコントローラ&テストモデルを作成しました。私はモデルでメソッドを作成し、コントローラにアクセスしたいがアクセスできないようにしたい。YII 2フレームワークでモデルからメソッドを呼び出す方法は?
テストコントローラ
<?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;
}
}
テストモデル
<?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;
}
}
私はフォーラムを検索しようとしたが、解決できない、あなたは私が解決するのに役立つことができますしてください?
ありがとうございます。
感謝。私は試してみましたが、今はメソッドにアクセスできますが、モデルのメソッドで何か問題があるように見えます。テストモデルでlistUserData()メソッドをチェックしてください。 – Purushottam