just starting with cakephp and almost with php after a few years without use it….
I have a table structure like this:
Parent table…… Clients:
<?php
class Cliente extends AppModel
{
var $name = 'Cliente';
var $validate = array(
'nombre' => array('rule'=>'notEmpty')
);
var $hasMany = array(
'Proyecto' => array(
'className' => 'Proyecto',
'foreignKey' => 'cliente_id',
'order' => 'Proyecto.nombre ASC',
'dependent' => true
)
);
}
?>
And child table: Proyects..
A client can have several projects assigned.
<?php
class Proyecto extends AppModel
{
var $name = 'Proyecto';
var $validate = array(
'nombre' => array('rule'=>'notEmpty')
);
//var $belongsTo = 'Cliente';
var $belongsTo = array(
'Cliente' => array(
'className' => 'Cliente',
'foreignKey' => 'cliente_id'
)
);
}
?>
Proyects only can be added (INSERT) from the index client view. So, I have this add (anadir) method:
function anadir($cliente_id, $nombre) {
if (!empty($this->data)) {
// echo "El código de cliente 22222: ". $this->data['Proyecto']['id'] . "</br>";
// echo "El código de cliente fk: ". $this->data['Proyecto']['cliente_id'] . "</br>" ;
// print_r($this->data);
//$this->data['Proyecto']['cliente_id'] = $cliente_id;
if ($this->Proyecto->save($this->data)) {
$this->Session->setFlash('El proyecto ha sido grabado.');
$this->redirect(array('controller'=>'clientes', 'action'=>'listar'));
}
} else {
//echo "El código de cliente 11111: ". $this->data['Proyecto']['id'];
$this->data['Proyecto']['cliente_id'] = $cliente_id;
}
$this->set('idcliente', $cliente_id);
$this->set('nombrecliente', $nombre);
}
I can show the parent client (id and number) in the ‘anadir.ctp’ view, but it always update the same proyect, changing the name and description only. No ADD/INSERT is done in the table, only UPDATE.
<!-- File: /app/views/proyectos/anadir.ctp -->
<h1>Añadir proyecto a cliente <?php echo "[$idcliente] - $nombrecliente"; ?></h1>
<?php
//echo $this->Form->create('Proyecto', array('action' => 'anadir'));
echo $this->Form->create('Proyecto', array('url' => '/proyectos/anadir/'.$idcliente.'/'.$nombrecliente)); // array('action' => 'anadir'));
echo $this->Form->input('cliente_id', array('type' => 'hidden'));
echo $this->Form->input('nombre');
echo $this->Form->input('descripcion', array('rows' => '10'));
echo $this->Form->end('Grabar proyecto');
echo $this->Html->link('Cancelar', array('controller'=>'clientes', 'action'=>'listar'));
?>
Surely it is a newbie question but I have tried almost everything.
Any idea ?
Thanks in advance.
==== Finally ====
After a few days trying to solve this it is running now. But I cannot understand the problem and I have no explanation why I have had that weird problem…
So I am going to look forward symfony2 Perhaps more difficult to start with but I hope more flexible (with multi Primary Keys and foreign key support, mainly) and clear than cakephp.
So it still looks (as deizel suggested) that you’ve got
idset in your$this->datasomehow, probably a field with mistaken in a form, not sure (you can post your view here so we can take a look). The best way to check it is to monitor actual SQL queries; put this after$this->Model->save()(without adding$this->Model->create()yet, for debug purposes):Add output of above to your question.
Regarding
$this->Model->create(): calling it prior to$this->Model->save()is perfectly ok and even recommended. It’s like variable initialization – you can skip it, but at your own risk, when you’re absolutely sure what you’re doing.To address
Missing argument 1 for ProyectosController::anadir()error you should post your URL here. It should be something like/proyectos/anadir/2/10.