aDvo
aDvo

Reputation: 904

Cakephp - HTML Form->Create for Controller without Model - result in errors

I have a controller called MCPController. I know this seems to go against Cakephp naming convention but

1) the name "MCPs" with the 's' did not make grammatical sense. So I left it as "singular"..

2) i don't actually have/need a Database table called MCP

This MCPController has an action "dayview" that calls a view called dayview.

In my dayview view I have the following code that creates a form:

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();

I get an error :

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

The error seems to be coming from the line with the "$this->Form->create" code, because taking away the Form->create seems to take the error away.

It seems to be expecting a table m_c_ps to exist in the database?

The form is meant to submit the new dropdown selection (via onchange event) back to the same Controller.

How can i fix this given that i don't actually have a table called 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)

Upvotes: 0

Views: 1671

Answers (2)

tremendusapps
tremendusapps

Reputation: 108

Well, when you break from Cake conventions, you get into all sorts of trouble ;) But there is help.

In your controller, you need to define that you're not using a model with the Controller::$uses property. Define it as empty where you have your class property declarations

public $uses = array();

Then cake will know NOT to look for a model. If you don't specify this, Cake defaults kick in and it tries to create a 'default' model called MCP and (per Cake convention) and ergo looks for a table called m_c_ps (also per convention)

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Form->create() expects model name as first parameter to it. So if you dont want to use model you could either try passing null or false to model parameter, Like:

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

or

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

Upvotes: 1

Related Questions