In Cakephp, why does the input method generate an empty selector?

1

In my application there are 2 related models and Shortcut and Role, where Role has several Shortcuts, the connection between the two normally occurs, except when trying to create an input in the "save" view of the controller Shortcut to select a Role existing.

Model Shortcut

<?php
class Shortcut extends AppModel
{
public $name = 'Shortcut';
public $diplayField = 'title';  
public $useTable = 'shortcuts';
public $belongsTo = array( 'Role' => array('className' => 'Role','foreignKey' =>          'role_id' ) );
}

?>

Model Role

class Role extends AppModel
{
public $name ='Role';
public $useTable = 'roles';
public $displayField = 'title';

public function getAdminRole()
{
    return 3;
}
public function getUserRole()
{
    return 2;
}
public function getPublicRole()
{
    return 1;
}
}

View / Shortcuts / Save

<?php echo $this -> Form -> create('Shortcut'); ?>
<table>
<tr>
    <td>
        <label>TÍTULO</label>
    </td>
    <td>
        <?php echo $this -> Form -> input('title',array('label'=>null));?>
    </td>
</tr>   
<tr>
    <td>
        <label>LINK</label>
    </td>
    <td>
        <?php echo $this -> Form -> input('link',array('label'=>null));?>
    </td>
</tr>
<tr>
    <td>
        <label>PERMISSÃO</label>
    </td>
    <td>
        <?php echo $this -> Form -> input('role_id',array('label'=>null));?    >
    </td>
</tr>
<tr>
    <td>
        <?php echo $this -> Form ->      submit('Enviar',array('controller'=>'shortcuts','action'=>'save'));?>
    </td>
</tr>   
</table>
<?php echo $this ->  Form -> end();?>
    
asked by anonymous 22.06.2014 / 21:09

1 answer

2

There is a convention: If there is a variable with the plural name of the related model in the view, it populates the dropdown options by itself. For example, put this in the controller, at Shortcuts::save :

$this->set('roles', $this->Shortcut->Role->find('list'));

You can also force the list of options by passing an array in the 'options' key:

echo $this->Form->input('role_id', array(
    'label' => null,
    'options' => array(
        1 => 'Public',
        2 => 'User',
        3 => 'Admin'
    )
));
    
22.06.2014 / 23:14