I've been playing with CakePHP form helper ( cakephp doc ) with some success. Now I'm tring to create a form located in a view not related to the model of the table. I followed the doc, and put that in my view:
<?php echo $this->Form->create('ApplicationMemberships', array(
'url' => array('controller' => 'ApplicationMemberships', 'action' => 'add'))); ?>
<fieldset >
<?php
echo $this->Form->input('chantier_id');
echo $this->Form->input('application_id');
echo $this->Form->input('ponderation',array('type' => 'number'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
I put this code in the controller to get the data populating the fields:
$chantiers = $this->Chantier->find('list');
$this->loadModel('Application');
$applications = $this->Application->find('list');
$this->set(compact('chantiers', 'applications'));
Everything went well: I have form, I put some data, then I have a flash saying the data has been saved. Problem: the data has not been added in the database.
Edit:
Here is the code doing the saving:
public function add() {
if ($this->request->is('post')) {
$this->ApplicationMembership->create();
if ($this->ApplicationMembership->save($this->request->data)) {
$this->Session->setFlash(__('The application membership has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The application membership could not be saved. Please, try again.'));
}
}
$chantiers = $this->ApplicationMembership->Chantier->find('list');
$applications = $this->ApplicationMembership->Application->find('list');
$this->set(compact('chantiers', 'applications'));
}
The error was the extra s in create('ApplicationMemberships')
.
According to the conventions of Cakephp, controllers should be plural and Model singular. So in order to create an instance of the associated model, I should have written:
create('ApplicationMembership')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.