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
an
please use
//In controller
$data=$this->Model->find('list');
$this->set('data',$data);
In View :
$this->Form->input('list',array("options"=>$data));
Or You can use this:
In Model:
/**
* Get list of choises from collumn SET or ENUM type
*
* @param string $sColumn - col name
* @param string $sTable - if use other table as Model
* @return array
*/
function fGetArrayFromSQLSet($sColumn, $sTable=null) {
# Pokud neni urcena tabulka, pouzij tabulku modelu
if( !$sTable ) {
$sTable= $this->useTable;
}
# Nacti nastaveni daneho pole dane tabulky
$tmpHlaseno=$this->query("SELECT COLUMN_TYPE
FROM information_schema.columns
WHERE TABLE_NAME = '$sTable'
AND COLUMN_NAME = '$sColumn'
"); //db($tmpHlaseno);
$tmpList= $tmpHlaseno[0]['columns']['COLUMN_TYPE'];
# Ziskej hodnoty z nastaveni a uloz je do pole
$sTmp= str_replace(')', '', str_replace('set(', '', str_replace('enum(', '', $tmpList)));
$aTmp= explode(',', str_replace("'", '', $sTmp));
foreach( $aTmp as $value ) {
$aSetList[$value]= $value;
} //db($aSetList);
return $aSetList;
} // END of fGetArrayFromSQLSet
In Controller:
# TB ENUM fields
$aFields= array('order', '<TB_col_SET_type>', '<TB_col_SET_type>', .... );
foreach ($aFields as $sFieldName ) {
$this->set(Inflector::pluralize(Inflector::variable($sFieldName)), $this->PHlaseni->fGetArrayFromSQLSet($sFieldName) );
}
In View is enought:
<?=$form->input('order')?>
<?=$form->input('<TB_col_SET_type>')?>
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.
Using CakePHP 3.6
$fruits = ['1'=>'orange','2'=>'melon','3'=>'lemon','4'=>'apple'];
echo $this->Form->control('Fruit', ['options'=>$fruits, 'label'=>"select your fruit", 'value'=>'lemon']);
Your dropdownlist will come with 'lemon' selected by default.
This code will produce the following html:
<div class="input select">
<label for="Fruit">select your fruit</label>
<select name="Fruit" id="Fruit">
<option value="1">orange</option>
<option value="2">melon</option>
<option value="3">lemon</option>
<option value="4">apple</option>
</select>
</div>
You can find more info here:
https://book.cakephp.org/3.0/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls
Assuming your model is User and the field you want to use is a list of US states (for example)...
In your controller:
$this->set('states',$this->State->find('list'));
and in your view:
<?php echo $form->input('User.state',array('type'=>'select','options'=>$states)); ?>
This is the right solution in my opinion:
of choices from column SET
or ENUM
type
*
* @param string $sColumn - col name
* @param string $sTable - if use other table as Model
* @return array
*/
function fGetArrayFromSQLSet($sColumn, $sTable=null) {
# Pokud neni urcena tabulka, pouzij tabulku modelu
if( !$sTable ) {
$sTable= $this->useTable;
}
# Nacti nastaveni daneho pole dane tabulky
$tmpHlaseno=$this->query("SELECT COLUMN_TYPE
FROM information_schema.columns
WHERE TABLE_NAME = '$sTable'
AND COLUMN_NAME = '$sColumn'
"); //db($tmpHlaseno);
$tmpList= $tmpHlaseno[0]['columns']['COLUMN_TYPE'];
# Ziskej hodnoty z nastaveni a uloz je do pole
$sTmp= str_replace(')', '', str_replace('set(', '', str_replace('enum(', '', $tmpList)));
$aTmp= explode(',', str_replace("'", '', $sTmp));
foreach( $aTmp as $value ) {
$aSetList[$value]= $value;
} //db($aSetList);
return $aSetList;
} // END of fGetArrayFromSQLSet