Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePhp:如何显示多个枚举值列表中的特定枚举值_Php_Cakephp_Enums - Fatal编程技术网

CakePhp:如何显示多个枚举值列表中的特定枚举值

CakePhp:如何显示多个枚举值列表中的特定枚举值,php,cakephp,enums,Php,Cakephp,Enums,我想获取并显示特定枚举值的下拉列表 例如,我有一个名为status的列。使用枚举值:-“打开”、“读取”、“进度”、“工件”、“提升”、“协调”、“完成”、“关闭” 我想要一种方法,用于选择并仅显示下拉列表中关闭的枚举值 我已经编写了一个主要方法来获取应用程序模型中的枚举值,如下所示 public function getEnumValues($columnName=null, $respectDefault=false ,$blank =false){ if ($columnN

我想获取并显示特定枚举值的下拉列表

例如,我有一个名为status的列。使用枚举值:-“打开”、“读取”、“进度”、“工件”、“提升”、“协调”、“完成”、“关闭”

我想要一种方法,用于选择并仅显示下拉列表中关闭的枚举值

我已经编写了一个主要方法来获取应用程序模型中的枚举值,如下所示

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版本。。。。如果你写下你的评论作为答案,那就太好了。。。所以我可以投票支持你的解决方案@做记号