1

So I started looking at CakePHP, and I'm just doing a few test cases to see if CakePHP will cater for all my needs.

I have a custom table (Table name: roles):

id  role    sub_site
1   Role 1    0
2   Role 2    0
3   Role 3    0
4   Role 4    0
5   Role 1    1

In my controller (User controler) I have the following code based on all the feedback here.

$this->loadModel("roles");
$rolesList = $this->roles->find('list', array(
        'conditions' => array('sub_site' => '0'),
        'fields' => array('id', 'role')
));
$this->set(compact('rolesList'));


I have the following in the view

$this->Form->input('role', array('label'=>false, 
               'div'=>false, 
               'type'=>'select', 
               'empty'=>'-- select one --', 
               'options'=>$rolesList));

Yet the output looks like this:

<select name="role" required="required" id="role">
<option value="">-- select one --</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

All I want is the output as follows:

<select name="role" required="required" id="role">
<option value="">-- select one --</option>
<option value="1">Role 1</option>
<option value="2">Role 2</option>
<option value="3">Role 3</option>
<option value="4">Role 4</option>
</select>

Please indicate what is wrong here as this is suppose to be simple to just populate?

6
  • seems like all good..post the debug value of $rolesList , how the data are actually coming ? Commented Nov 15, 2016 at 17:09
  • sorry but how do I get the debug values, I'm new to this framework :-( Commented Nov 15, 2016 at 17:37
  • Hope this helps: @ManoharKhadka SELECT roles.id AS roles__id, roles.role AS roles__role FROM roles roles WHERE sub_site = 0 rolesList(array) 11 22 33 44 CREATE TABLE roles ( id int(11) NOT NULL, role varchar(255) NOT NULL, sub_site int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO roles (id, role, sub_site) VALUES (1, 'Recruiter', 0), (2, 'Moderator', 0), (3, 'Line Manager', 0), (4, 'Admin', 0), (5, 'Super Admin', 1); Commented Nov 15, 2016 at 17:57
  • 1
    Check this doc: Finding Key/Value Pairs you can pass yur own keyField and valueField Commented Nov 15, 2016 at 18:03
  • Nope tried that. Still only getting the ID's as the option values and option fields. $this->set('rolesList', $this->roles->find('list', [keyField => 'id', valueField => 'role'])); <select name="role" required="required" id="role"><option value="">-- select one --</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select> Commented Nov 15, 2016 at 18:17

1 Answer 1

1

I am getting results using this method. In my controller:

$articles = TableRegistry::get('Transfers');
$make=$articles->find('list', ['keyField' => 'id','valueField' => 'make']);
$this->set('make',$make);

and then in view:

echo $this->Form->input('make',['type'=>'select','options'=>$make ,'empty'=>'-Select Make-','class'=>'form-control','templateVars'=>['class'=>'col-md-4']]);

Hope you will get results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.