1
votes

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');
    }
}
?>
1
you want the view to show a list (or similar) of staffs available to link the new pending? am I understanding you correctly? If so... well, you need to show the corresponding action in the controller, since I don't see you are passing that list to the view anywhere. - Nunser
Yes is correct, when i make a new pending with add, i want to assign a staff to it by showing a select with it's names. I thought u didn't have to pass a list to the action, cuz cake knows of this list through the model relation. But I'm gonna check that. Thanks Nunser!! - Gonzalo

1 Answers

0
votes

Cake's magic works ... with a condition...

You are correct in thinking that maybe, because of model relations been set correctly, it should just... work. But you are missing one part: passing the list to the view. That part is not magical.

Read this part of the docs (almost all the way down before inputs starts)

If you want to create a select field while using a belongsTo - or hasOne - Relation, you can add the following [...]

(and now putting what you should put...)

$this->set('staffs', $this->Pending->Staff->find('list'));

Remarks: note the variable name, is the model in plural. And then you put a list to that variable.

And in the view, you do what you are doing

echo $this->Form->input('staff_id', array('label' => 'Asignar a miembro'));

And you should be done :)

[EDIT]
Just saw your last edit. You are missing

$this->set('staffs', $staffs);
//or
$this->set(compact('staffs'));