Hi guys,
I'm trying to edit data in related tables (Material hasOne
Book). But by the time to save the data in the table are well edited,
but the system tries to save a new record in paper, instead of
edit.
See code below:
BensController
------------------------------------------------------------------
function edit($id = null)
{
$this->Material->id = $id;
if (empty($this->data))
{
$this->data = $this->Material->read();
}
else
{
$material = $this->Material->save($this->data);
if (!empty($material))
{
switch ($this->data['Material']['material_type_id'])
{
case "2":
$this->editBook($this->Bem->id);
break;
default:
$this->Session->setFlash('Material edited.');
$this->redirect(array('action' => 'index'));
break;
}
}
}
}
function editBook($id = null)
{
$this->Material->Book->material_id = $id;
if (empty($this->data))
{
$this->data = $this->Material->Book->read();
}
else
{
if ($this->Material->Book->save($this->data))
{
$this->Session->setFlash('Book edited.');
$this->redirect(array('action' => 'index'));
}
}
}
Alterar View
--------------------------------------------------------------------------------------
<fieldset>
<legend>Update Material</legend>
<div id="div-form-edit">
<?php
echo $form->create('Material', array('action'=>'edit'));
echo $form->input('Material.id', array('type'=>'hidden'));
echo $form->input('Material.material_type_id', array
('type'=>'hidden'));
echo $form->input('Material.title');
echo $form->input('Material.color');
?>
<div id="div-form-book">
<?php
echo $form->input('Book.material_id', array
('type'=>'hidden'));
echo $form->input('Book.isbn');
echo $form->input('Book.title');
?>
</div>
<?php
echo $form->end('Save');
?>
</div>
</fieldset>
Material Model
--------------------------------------------------------------
var $hasOne = array(
'Book' => array(
'className' => 'Book',
'foreignKey' => 'material_id'
),
Erro
----------------------------------------------------------------
Warning (512): SQL Error: 1062: Duplicate entry '7' for key 1 [CORE/
cake/libs/model/datasources/dbo_source.php, line 525]
Code | Context
$sql = "INSERT INTO `books` (`material_id`, `isbn`, `title`)
VALUES (7, '1234567890123', 'test')"
$error = "1062: Duplicate entry '7' for key 1"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---