yii2在控制器中获取模型

时间:2016-03-21 19:13:55

标签: php yii2

我有一个yii2项目和控制器MyModelController.php,我想获得模型

views
  posts
    post_comments
      ...html.erb
    post_comment_replies
      ...html.erb

和模型

    namespace app\controllers;
    use yii\web\Controller;
    use yii\db\Command;
    class MyModelController extends Controller
    {
        public function actionPhoto()
        {
            $model=new myModel;
            return $this->render('photo', ['model' => $model]);
        }
   }

他们分别在 MyModel.php: use namespace app\models; use yii\base\Model; class MyModel extends ActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'my_model'; } public function rules() { ... some rules } public function relations() { return array( ); } //like fields in my table in mysql public function attributeLabels() { return array( 'id' => 'ID', 'my_model_atribute' => 'MyModel', ); } public function search() { ... some search } } /basic/models

当我在我的观点中称呼它时,我犯了这个错误:

/basic/controllers

UPD1 我试图在Class 'app\controllers\MyModel' not found

中查看
photo.php

我仍然错误地认为没找到这门课。 的 UPD2 我将命名空间更改为use yii\helpers\Html; $model=new MyModel; var_dump($model); basic\models,因为它实际上是,但它仍然没有在那里找到类/

3 个答案:

答案 0 :(得分:3)

您必须先使用模型。

use yii\helpers\Html;
use basic\models\Mymodel
$model=new MyModel;
var_dump($model);

或者

use yii\helpers\Html;
$model=new \basic\models\MyModel;
var_dump($model);

答案 1 :(得分:1)

    namespace basic\controllers;
    use yii\web\Controller;
    use yii\db\Command;
    class MyModelController extends Controller
    {
        public function actionPhoto()
        {
            $model=new myModel;
            return $this->render('photo', ['model' => $model]);
        }
   }

MyModel.php:         使用namespace basic \ models;

use yii\base\Model;
class Model extends ActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function tableName()
    {
        return 'my_model';
    }
    public function rules()
    {
        ... some rules
    }
    public function relations()
    {
        return array(
        );
    }
    //like fields in my table in mysql
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'my_model_atribute' => 'MyModel',
        );
    }

    public function search()
    {
       ... some search
    }
}

答案 2 :(得分:-1)

如果您的代码是您会得到错误

app\controllers\MyModel

因为您的模型(MyModel)位于/models/MyModel.php文件夹中,并且具有命名空间app \ models

所以你必须打电话给

use app\models\MyModel;

并用

声明
$model = new MyModel();

    namespace app\controllers;

    use yii\web\Controller;
    use yii\db\Command;

    // this to call your models with namespace
    use app\models\MyModel;

    class MyModelController extends Controller
    {
        public function actionPhoto()
        {
            // this to declare your model
            $model = new myModel();

            return $this->render('photo', ['model' => $model]);
        }
   }