cakephp - form for belongsTo Model
Posted
by
user1511579
on Stack Overflow
See other posts from Stack Overflow
or by user1511579
Published on 2012-12-04T11:02:38Z
Indexed on
2012/12/04
11:03 UTC
Read the original article
Hit count: 180
I createt the following model to link 2 relational tables:
class Ficha extends AppModel {
//public $useTable = 'ficha_seg';
var $primaryKey = 'id_ficha';
var $name = 'Ficha';
var $belongsTo = array(
'Perigo' => array(
'className' => 'Perigo',
'foreignKey' => false,
'conditions' => 'Perigo.id_fichas = Ficha.id_ficha'
)
);
}
Now, i have a form that requires data from the class Ficha, and then is redirected to another ctp page where i will input the data for the table "Perigos". However, since i'm still a newbie in cakephp i'm having difficult building that second form to insert the data on the table "Perigos". Here goes the code i built at the moment related to the second form:
FichasController.php (the method where is it supposed to save the data on the table "Perigos":
public function preencher_ficha(){
if ($this->request->is('ficha')) {
$this->Ficha->create();
if ($this->Ficha->Perigo->save($this->request->data)) {
$last_id=$this->Ficha->getLastInsertID();
$this->Session->setFlash('Your post has been updated '.$last_id.'.');
//$this->redirect(array('action' => 'preencher_ficha'));
} else {
$this->Session->setFlash('Unable to qualquer coisa your post.');
}
}
}
The preencher_ficha.ctp file with the form:
echo $this->Form->create('Ficha->Perigo', array('action' => 'index'));
echo $this->Form->input('class_subst', array('label' => 'Classificação:'));
echo $this->Form->input('simbolos_perigo', array('label' => 'Símbolos:'));
echo $this->Form->input('frases_r', array('label' => 'Frases:'));
echo $this->Form->end('Finalizar Ficha');
Here i guess the create part is wrong, but i think i have errors too in the controller part.
© Stack Overflow or respective owner