CakeHTML表单->;为没有模型的控制器创建-导致错误


CakeHTML Form->Create for Controller without Model - result in errors

我有一个名为MCPController的控制器。我知道这似乎违反了Cakephp的命名约定,但

1) 带"s"的名称"MCP"在语法上没有意义。所以我把它留作"单数"。。

2) 我实际上没有/需要一个名为MCP 的数据库表

这个MCPController有一个操作"dayview",它调用一个名为dayview的视图。

在我的日视图中,我有以下创建表单的代码:

echo $this->Form->create('MCP', array('formnovalidate' => true, 'controller'=>'MCP', 'action'=>'merchantonchange'));
echo $this->Form->input('MerchantAssignments', array('label' => 'Merchant Assignments',
                                                    'type' => 'select',
                                                    'options'=>$this->Session->read('Auth.MerchantAssignments'),
                                                    'default' => $this->Session->read('Auth.ActiveMerchant'),
                                                    'onChange' => 'this.form.submit()'));
echo $this->Form->end();

我得到一个错误:

Error: Table m_c_ps for model MCP was not found in datasource default.

错误似乎来自"$this->Form->create"代码行,因为去掉Form->creates似乎就消除了错误。

它似乎期望数据库中存在一个表m_c_ps?

该表单旨在将新的下拉选择(通过onchange事件)提交回同一个Controller。

如果我实际上没有一个名为m_c_ps的表,我该如何解决这个问题?

Stacktrace:

CORE'Cake'Model'Model.php line 3498 → Model->setSource(string)
CORE'Cake'Model'Model.php line 1355 → Model->getDataSource()
CORE'Cake'View'Helper'FormHelper.php line 207 → Model->schema()
CORE'Cake'View'Helper'FormHelper.php line 459 → FormHelper->_introspectModel(string, string)
APP'View'Layouts'default.ctp line 191 → FormHelper->create(string, array)
CORE'Cake'View'View.php line 935 → include(string)
CORE'Cake'View'View.php line 897 → View->_evaluate(string, array)
CORE'Cake'View'View.php line 529 → View->_render(string)
CORE'Cake'View'View.php line 474 → View->renderLayout(string, string)
CORE'Cake'Controller'Controller.php line 952 → View->render(null, null)
CORE'Cake'Routing'Dispatcher.php line 192 → Controller->render()
CORE'Cake'Routing'Dispatcher.php line 160 → Dispatcher->_invoke(MCPController, CakeRequest, CakeResponse)
APP'webroot'index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)

好吧,当你打破Cake惯例时,你会遇到各种各样的麻烦;)但是有帮助。

在控制器中,您需要定义您没有使用具有controller::$uses属性的模型。在类属性声明的地方将其定义为空

public $uses = array();

然后蛋糕就会知道不要找模特。如果你不指定这一点,Cake默认值就会生效,它会尝试创建一个名为MCP和(根据Cake约定)的"默认"模型,ergo会查找一个名称为m_c_ps(也根据约定)的表

Form->create()需要模型名称作为它的第一个参数。因此,如果你不想使用模型,你可以尝试将nullfalse传递给模型参数,比如:

echo $this->Form->create(null, array('formnovalidate' => true, 'controller'=>'MCP', 'action'=>'merchantonchange'));

echo $this->Form->create(false, array('formnovalidate' => true, 'controller'=>'MCP', 'action'=>'merchantonchange'));