I'm new to cakephp, and i followed some tutorials on it. Now I'm trying to build my first app, and I'm stucked with this relation 'belongsTo'. That is not working as it should, more likely I'm doing something wrong. So here are my tables and my models and controllers.
This is the view that doesn't show any staff id's or names. And it's driving me crazy!!!. Located under app/View/Pendings/add.ctp:
<div class='form'>
<?php echo $this->Form->create('Pending'); ?>
<fieldset>
<legend>Nueva Tarea</legend>
<?php
echo $this->Form->input('subject', array('label' => 'Asunto'));
echo $this->Form->input('body', array('label' => 'Descripcion', 'rows' => '3'));
echo $this->Form->input('staff_id', array('label' => 'Asignar a miembro'));
?>
</fieldset>
<?php echo $this->Form->end('Asignar'); ?>
</div>
where staff_id is the foreign key for this model Pending. And corresponds to id in the staffs table. So what I tried to do is Staff hasMany Pending, and Pending belongsTo Staff. Because Pending is a sort of pending task for the staff. That's why staff has many pendings.
Here is the Pending model, in app/Model/Pending.php with the 'belongsTo' relation:
<?php
class Pending extends AppModel {
public $validate = array(
'subject' => array(
'rule' => 'notEmpty',
'message' => 'Debe ingresar un Asunto.'
),
'body' => array(
'rule' => 'notEmpty',
'message' => 'Debe ingresar una Descripción.'
)
);
public $belongsTo = array(
'Staff' => array(
'className' => 'Staff',
'foreignKey' => 'staff_id'
)
);
}
?>
Here is the Staff model, in app/Model/Staff.php with the 'hasMany' relation:
<?php
class Staff extends AppModel {
public $displayField = 'name';
public $validate = array(
'last_name' => array(
'rule' => 'notEmpty'
),
'name' => array(
'rule' => 'notEmpty'
),
'passwd' => array(
'rule' => 'notEmpty'
),
'email' => array(
'rule' => 'notEmpty'
)
);
public $hasMany = array(
'Pending' => array(
'className' => 'Pending',
'foreignKey' => 'staff_id'
)
);
}
?>
and finally my two tables, staffs and pendings
mysql> describe staffs;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| name | varchar(40) | NO | | NULL | |
| last_name | varchar(40) | NO | | NULL | |
| email | varchar(40) | NO | | NULL | |
| password | varchar(20) | YES | | NULL | |
| active | tinyint(1) | YES | | 1 | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
mysql> describe pendings;
+----------+------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| staff_id | int(10) unsigned | YES | MUL | NULL | |
| created | datetime | NO | | NULL | |
| pdate | datetime | YES | | NULL | |
| subject | varchar(250) | YES | | NULL | |
| body | tinytext | YES | | NULL | |
| state | enum('pending','done','reasigned') | YES | | pending | |
+----------+------------------------------------+------+-----+---------+----------------+
I think i followed cakephp conventions. I really can't see my mistake. Thanks so much for your help.
Sorry, here is the controller (app/Controller/PendingsController.php):
<?php
class PendingsController extends AppController{
public function index(){
$this->Pending->recursive = 0;
$this->set('pendings', $this->paginate());
}
public function add(){
if( $this->request->is('post') )
{
if( $this->Pending->save($this->request->data) )
{
$this->Session->setFlash('Tarea Asignada');
$this->redirect(array('action' => 'index'));
}
}
$staffs = $this->Pending->Staff->find('list');
}
}
?>