Does anybody know how to select the contents of one take from a different view in CakePHP?
I have a take itemgroups
that has 2 fields ID
and Description
. I need to make a down down list in the item add page but I can not find a good way to get all of the values from another table into an array to put into the page.
Below I have also listed my models for each.
<?php
class Item extends AppModel
{
var $name = 'Item';
var $belongsTo = 'Itemgroup';
}
?>
class Itemgroup extends AppModel
{
var $name = 'Itemgroup';
var $hasOne = array('Item');
var $validate = array(
'description' => array(
'rule' => 'notEmpty'
),
'description' => array(
'rule' => 'notEmpty'
)
);
}
?>
Here is the code to display a select dropdown.
where $cate is loaded with an array from a find('list') in the format
array(0 => 'option1', 1=>'option2', etc etc etc
Or You can use this:
In Model:
In Controller:
In View is enought:
Assuming your model is User and the field you want to use is a list of US states (for example)...
In your controller:
and in your view:
If it's something like a "US States" dropdown list that is going to be repeated from page to page, consider using an Element, which you can just pass data to and you won't have to repeat all the UI code again.
In controller:
In View:
Using CakePHP 3.6
Your dropdownlist will come with 'lemon' selected by default.
This code will produce the following html:
You can find more info here:
https://book.cakephp.org/3.0/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls