다음과 같이 할 수 있습니다. 컨트롤러 동작에서 목록을 설정하고,보기에서 사용하고, Model :: schema() 메서드에서 목록을 가져 와서 각 값을 "humanize"합니다. B : 할 수있는 도우미 메서드를 작성합니다. 지금 모든 헬퍼가 getHumanFieldNames 방법이
<?php
class AppHelper extends Helper {
/**
* Returns and array of 'column_name' => 'Column Name' values from a model
*
* @param string $modelName
* @param type $includeModelName If true, prepends the model name to the field value
* @return array Or false if Model is unavailable
*/
function getHumanFieldNames($modelName, $includeModelName = true){
$model = ClassRegistry::init($modelName, true);
if (!$model) return false;
$schema = $model->schema();
$return = array();
foreach($schema as $field => $meta){
$return[$field] =
($includeModelName) ?
Inflector::humanize($modelName) . ' ' .Inflector::humanize($field)
: Inflector::humanize($field);
}
return $return;
}
}
?>
을 : 당신은
이 그 일을 더 나은 방법이 될,하지만 난 그것을 찾을 때까지, 내 AppHelper 코드의 당신에게 노래를 할 수 있습니다에 대한 보기에서 사용하려면 다음과 같이 사용하십시오.
<?php
$accountFieldNames = $this->Form->getHumanFieldNames('account');
// returns array('owner' => 'Account Owner', 'name' => 'Account Name', 'bank_code' => 'Account Bank code')
//you can use any helper instead of $this->Form
//or better yet, you could write your own "utility" helper
echo $this->Form->select('database_column', $accountFieldNames);
// renders a select element with all
// model column names as values and
// human readable names as labels
?>
나는 당신의 편의를 위해, 두 번째 매개 변수로 부울 플래그를 추가 한
:
<?php
$this->Form->getHumanFieldNames('account', false);
//returns array('name' => 'Name', 'bank_code' => 'Bank Code')
$this->Form->getHumanFieldNames('account', true); // default is true
//returns array('name' => 'Account Name', 'bank_code' => 'Account Bank Code')
?>