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

Asked

Viewed 61 times

1

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

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();?>

1 answer

2


It has a convention: if there exists in the view a variable with the plural name of the related model, it fills the dropdown options by itself. For example, put this in the controller, in Shortcuts::save:

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

It is also possible to force the list of options by passing an array in the key 'options':

echo $this->Form->input('role_id', array(
    'label' => null,
    'options' => array(
        1 => 'Public',
        2 => 'User',
        3 => 'Admin'
    )
));
  • then, but in a previous project I just input the field referencing the other table, and it was already included in the options box, all the records of that table

  • 1

    I found a business I didn’t know on manual, and edited the answer. See if this resolves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.