Reputation: 337
How can i get categories names to the select list not their ID's !! , I'm using CakePHP
the 'categories' table has only two columns (id, name)
the view part :
echo $this->Form->input('category',array(
'type' => 'select',
'options' => $categories,
'empty' => 'select category'
));
the association :
class Job extends AppModel{
public $name = 'Job';
public $belongsTo = array('Category');
}
and the controller :
$categories= $this->Job->Category->find('list');
$this->set('categories',$categories);
the result is a select list with 1,2,3,4,5,6 values
Upvotes: 0
Views: 1741
Reputation: 4776
In cakephp 3.X
// Common Usage:
$users = [
['id' => 1, 'name' => 'mark'],
['id' => 2, 'name' => 'jane'],
['id' => 3, 'name' => 'sally'],
['id' => 4, 'name' => 'jose'],
];
$results = Hash::extract($users, '{n}.id');
// $results equals:
// [1,2,3,4];
https://book.cakephp.org/3.0/en/core-libraries/hash.html
Upvotes: 0
Reputation: 43
you need to use virtual field in cakephp.
http://book.cakephp.org/2.0/en/models/virtual-fields.html
also Line main in ctp file
foreach($times as $key => $value ){ $timesList[$value] = $value; } $times = $timesList
use as per your model-controller. for mor info. plz follow this link .here i face same problem and i got solution.
virtual field are not working in cakephp
Upvotes: 0
Reputation: 481
You needs to edit your controller code as following :
$categories= $this->Job->Category->find('list',array('fields'=>array('Category.id','Category.name')));
Upvotes: 1