I'm building a NewsManager
plugin that has a NewsController
in the Admin
prefix.
I want this controller could both be used as it is (with the route /admin/news-manager/news/*
) or as a parent controller that could be overload by inherance.
Here is my code :
// plugins/NewsManager/Controller/Admin/NewsController.php
public function index()
{
$modelClass = $this->modelClass;
$news = $this->$modelClass->find()
->order(['created' => 'DESC']);
$this->set(compact('news'));
}
I want to dynamically get the modelClass in order to make inheritance easier.
When I go to /admin/news-manager/news
, the $modelClass
value above returns 'NewsManager.News'
and then the find()
request returns the error Undefined property: NewsController::$NewsManager.News
How could I do to avoid having the plugin prefixed in modelClass
?
CodePudding user response:
Use loadModel()
(or as of 4.3 fetchTable()
) without any arguments, that will then load the default model set for the controller, eg:
$this->loadModel()->find()
or:
$this->fetchTable()->find()