简体   繁体   中英

yii2 get model in controller

I have an yii2 project and controller MyModelController.php there, where I want to get model

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]);
    }
}

and 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
        }
    }

They are in /basic/models and /basic/controllers respectively.

And I get this mistake when I call it in my view:

Class 'app\controllers\MyModel' not found

UPD1 I tried to make it in view photo.php

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

And I still gives mistake that class not found. UPD2 I change namespace to basic\\models and public_html\\basic\\models as it actually are but it still doesn't find class there/

You have to use model first.

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

Or

use yii\helpers\Html;
$model=new \basic\models\MyModel;
var_dump($model);
    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: use 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
    }
}

You will get error if your code is

app\controllers\MyModel

because your model ( MyModel ) is on folder /models/MyModel.php and with namespace app\\models

so u must call with

use app\models\MyModel;

and declare it with

$model = new MyModel();

Like This

    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]);
        }
   }

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