PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] CakePHP 3 - populate select dropdown with data from custom table

 March 09, 2022     cakephp-3.0     No comments   

Issue

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?


Solution

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.



Answered By - Sandep
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing