CakePhp:如何显示多个枚举值列表中的特定枚举值
我想获取并显示特定枚举值的下拉列表 例如,我有一个名为status的列。使用枚举值:-“打开”、“读取”、“进度”、“工件”、“提升”、“协调”、“完成”、“关闭” 我想要一种方法,用于选择并仅显示下拉列表中关闭的枚举值 我已经编写了一个主要方法来获取应用程序模型中的枚举值,如下所示CakePhp:如何显示多个枚举值列表中的特定枚举值,php,cakephp,enums,Php,Cakephp,Enums,我想获取并显示特定枚举值的下拉列表 例如,我有一个名为status的列。使用枚举值:-“打开”、“读取”、“进度”、“工件”、“提升”、“协调”、“完成”、“关闭” 我想要一种方法,用于选择并仅显示下拉列表中关闭的枚举值 我已经编写了一个主要方法来获取应用程序模型中的枚举值,如下所示 public function getEnumValues($columnName=null, $respectDefault=false ,$blank =false){ if ($columnN
public function getEnumValues($columnName=null, $respectDefault=false ,$blank =false){
if ($columnName==null) { return array(); } //no field specified
//Get the name of the table
$db = ConnectionManager::getDataSource($this->useDbConfig);
$tableName = $db->fullTableName($this, false);
//Get the values for the specified column (database and version specific, needs testing)
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");
//figure out where in the result our Types are (this varies between mysql versions)
$types = null;
if ( isset( $result[0]['COLUMNS']['Type'] ) ) { $types = $result[0]['COLUMNS']['Type']; $default = $result[0]['COLUMNS']['Default']; } //MySQL 5
elseif ( isset( $result[0][0]['Type'] ) ) { $types = $result[0][0]['Type']; $default = $result[0][0]['Default']; } //MySQL 4
else { return array(); } //types return not accounted for
//Get the values
$values = explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types) );
if($respectDefault){
$assoc_values = array("$default"=>Inflector::humanize($default));
foreach ( $values as $value ) {
if($value==$default){
continue;
}
$assoc_values[$value] = __t(Inflector::humanize($value));
}
}else{
$assoc_values = array();
if($blank){
$assoc_values[] = '-- انتخاب نشده --';
}
foreach ( $values as $value ) {
$assoc_values[$value] = __t(Inflector::humanize($value));
}
}
return $assoc_values;
} //end getEnumValues
我将不得不为这个问题找到解决方案我试图阻止mysql枚举,因为它们在CakePHP中本机不受支持 相反,尝试使用类似tinyint(2)的方法和简单的静态模型方法来模拟它。
它更快、更动态。为什么这么复杂?只需在您的模型中使用tinyint(2)和。4-5行代码:)PS:你应该经常提到你正在使用的cakephp的确切版本。谢谢,它起作用了。。。。我会记住从下次开始提到cakephp版本。。。。如果你写下你的评论作为答案,那就太好了。。。所以我可以投票支持你的解决方案@做记号