I have created such a situation class

class NewsService extends Model { /** * @param \common\models\News */ private $_newsModel; /** * NewsService constructor. * @param array $config */ public function __construct($config = []) { parent::__construct($config); if(isset($config['id']) && $config['id']) { $this->_newsModel = News::findOne($config['id']); $this->setAttributes($this->_newsModel->getAttributes()); } else { $this->_newsModel = new News(); } } public function rules() { return $this->_newsModel->rules(); } public function attributeLabels() { return $this->_newsModel->attributeLabels(); } 

In the controller I create this model, but the error takes off that there are no attributes

  public function actionCreate() { $model = new NewsService(); if (Yii::$app->request->isPost && $model->validate()) { $model->save(); return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', ['model' => $model]); } 

Getting unknown property: common \ models \ service \ NewsService :: title

What could be the problem?

    2 answers 2

    If database schema caching is enabled, you can try clearing the cache.

    You create the NewsService object without parameters, therefore in $model you will have a News object (you need the code of this class), then the render display is created, without validating and saving the model. In the display, you are most likely referring to the title field, which has some problems in News .

    • Yes, there seems to be no problems. The news model was generated using gii public function rules () {return [[['date_created', 'date_update'], 'safe'], [['title', 'description', 'content'], 'required'], [['status'], 'boolean'], [['article_or_news'], 'integer'], [['content', 'url', 'title', 'description', 'image' ], 'string'],]; } - insisted validation - gudfar
    • So we need the magic methods __get () and __set () to get the title field in the NewsService, since NewsService :: title does not exist, it lies in NewsService :: _ newsModel, which can be accessed in the display via $ model -> _ newsModel-> title - Kirill

    as it turned out, it was necessary to add all the attributes to the properties of the NewsService class

     <?php namespace common\models\service; use \common\models\News; use \yii\base\Model; use \common\models\service\Translit; class NewsService extends Model { public $id; public $date_created; public $date_update; public $title; public $description; public $content; public $image; public $isNewRecord; public $status; public $article_or_news; public $url; /** * @param \common\models\News */ private $_newsModel; /** * NewsService constructor. * @param array $config */ public function __construct($config = []) { parent::__construct($config); if(isset($config['id']) && $config['id']) { $this->_newsModel = News::findOne($config['id']); $this->setAttributes($this->_newsModel->getAttributes()); } else { $this->_newsModel = new News(); $this->attributes = $this->_newsModel->attributes; } } public function rules() { return $this->_newsModel->rules(); } public function attributeLabels() { return $this->_newsModel->attributeLabels(); } }