CakePHP 2插件控制器不';t加载插件模型
我的插件控制器“PagesController.php”不想加载它的相关模型。我总是遇到以下错误:CakePHP 2插件控制器不';t加载插件模型,php,model,cakephp-2.0,Php,Model,Cakephp 2.0,我的插件控制器“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 synta
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)
{
...
}
}
<?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();
...
}
}
似乎您的模型对象使用的是cakephp而不是插件创建的默认模型。如果没有在控制器文件中加载插件模型。请尝试使用public$uses=array('CoasterCms.Page')
加载插件的模型
<?php
class PagesController extends CoasterCmsAppController
{
public $uses = array('CoasterCms.Page');
....
}
检查以确保get\u class($this->Page)
返回预期的类型,而不是Cake的泛型模型类。感谢您的尝试!我已经在我的操作中加入了代码,但它并没有改变模型。当我进行调试时(get_类($this->Page));它只是说“页面”。这是一个很好的尝试来解决我的问题。但我已经试过了(我应该告诉你,对不起,我忘了这么做)。我终于找到了它不起作用的原因。在我的AppController中,我正在加载一个组件,在该组件中我有以下规则:$this->_page=ClassRegistry::init('page');我不知道为什么这会迫使标准页面模型加载到我的插件模型中。但是,嘿,我很高兴我在三天的搜索后找到了它:很好阅读。我也遇到了同样的问题,这也破坏了默认的关联规则。
<?php
class CoasterCmsAppModel extends Model {
public $actsAs = array(
'Containable'
);
}
<?php
class PagesController extends CoasterCmsAppController
{
public $uses = array('CoasterCms.Page');
....
}