我的插件控制器'PagesController.php'不想加载它的相关模型。我总是遇到以下错误:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'getPageById' at line 1
SQL Query: getPageById
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
我正在谈论的插件称为“ CoasterCms”。这是其中包含最重要的文件夹和文件的树:
-- Plugin
---- CoasterCms
------ Controller
-------- CoasterCmsAppController.php
-------- NewsArticlesController.php
-------- PagesController.php
------ Model
-------- CoasterCmsAppModel.php
-------- NewsArticle.php
-------- Page.php
------ View
-------- NewsArticles
---------- add.ctp
---------- edit.ctp
---------- delete.ctp
插件“ PagesController.php”:
<?php
class PagesController extends CoasterCmsAppController
{
public function index()
{
...
}
public function add()
{
...
}
public function edit($id = null)
{
$this->Page->getPageById(1);
}
public function delete($pageId = null)
{
...
}
}
插件“ CoasterCmsAppController.php”:
<?php
class CoasterCmsAppController extends Controller {
public $helpers = array(
'Html',
'Form',
'Session',
'CoasterCms.CmsMenu'
);
public $components = array(
'Session',
'Flash',
'Paginator',
'Auth' => array(
'loginAction' => array(
'plugin' => 'coaster_cms',
'controller' => 'users',
'action' => 'login'
),
'loginRedirect' => array(
'plugin' => 'coaster_cms',
'controller' => 'menus',
'action' => 'index'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
'authError' => 'You have no rights.'
)
);
public function beforeRender()
{
parent::beforeRender();
...
}
}
插件“ Page.php”:
<?php
class Page extends CoasterCmsAppModel
{
public $actsAs = array(
'Slugable.Slugable' => array(
'name' => 'slug'
)
);
public $hasOne = array(
'ModuleActionModule' => array(
'className' => 'CoasterCms.ModuleActionModule',
'foreignKey' => 'module_action_module_id'
)
);
public $hasMany = array(
'MenuPageLink' => array(
'className' => 'CoasterCms.MenuPageLink',
'foreignKey' => 'page_id'
)
);
public $validate = array(
...
);
/**
* Get a page.
*
* @param int $id Pagina id.
* @throws NotFoundException If id type is not numeric.
* @throws NotFoundException If page doesn't exists.
* @return array Pagina.
*/
public function getPageById ($id) {
if (!$id || !is_numeric($id)) {
throw new NotFoundException(
__('Invalid id.')
);
}
$page = $this->find('first', array(
'recursive' => -1,
'fields' => array(
'Module.id',
'Module.name',
'Module.controller'
),
'contain' => array(
'ModuleActionModule' => array(
'fields' => array(
'ModuleActionModule.id',
'ModuleActionModule.module_id',
'ModuleActionModule.module_action_id'
),
'ModuleAction' => array(
'fields' => array(
'ModuleAction.name',
),
'order' => array(
'ModuleAction.name' => 'asc'
)
)
)
),
'conditions' => array(
'Module.id' => $id
)
));
if (!$page) {
throw new NotFoundException(
__('Invalid page.')
);
}
return($page);
}
}
插件“ CoasterCmsAppModel.php”:
<?php
class CoasterCmsAppModel extends Model {
public $actsAs = array(
'Containable'
);
}
由于我的命名约定还可以,所以我对此问题感到头疼。有人可以帮我吗?
非常感谢!
答案 0 :(得分:1)
似乎您的模型对象使用的是Cakephp创建的默认模型,而不是插件创建的模型。如果您没有在控制器文件中加载插件模型。请尝试使用public $uses = array('CoasterCms.Page');
来加载插件的模型。
<?php
class PagesController extends CoasterCmsAppController
{
public $uses = array('CoasterCms.Page');
....
}
参考:https://book.cakephp.org/2.0/en/plugins/how-to-create-plugins.html#plugin-controllers