Zend Mysql 获取 ENUM 值

发布于 2024-10-01 06:20:46 字数 228 浏览 4 评论 0原文


我在我的应用程序中使用Zend Framework。我想知道如何从 MySQL 表中的 ENUM 字段获取值。
例如:我有 permissions 字段 (ENUM( '删除_管理员','编辑_管理员'))。如何以最好的方式获取 array('delete_admin', 'edit_admin') ?
提前谢谢您。

I use Zend Framework in my application. And I want to know how to get values from ENUM field in MySQL table.
For example: i have permissions field (ENUM('delete_admin', 'edit_admin')). How to get array('delete_admin', 'edit_admin') in he best way?
Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海未深 2024-10-08 06:20:48

将方法添加到 Zend_Db_Table_Abstract 扩展类中。

public function getEnumValues($field) {
  $metadata = $this->info(self::METADATA);
  preg_match_all('/\'(?<item>.+?)\'/', $metadata[$field]['DATA_TYPE'], $matches);
  return $matches['item'];
}

Add a method to your Zend_Db_Table_Abstract extended class.

public function getEnumValues($field) {
  $metadata = $this->info(self::METADATA);
  preg_match_all('/\'(?<item>.+?)\'/', $metadata[$field]['DATA_TYPE'], $matches);
  return $matches['item'];
}
桃气十足 2024-10-08 06:20:48

应用程序模型映射器的方法。假设 your_field_name 是您需要列出其 ENUM 值的数据库列的名称。

该方法返回一个数组,其中包含 your_field_name 列的 ENUM 值。

public function getAvailableEnumTypes() {
    $this->your_model_name = new Application_Model_DbTable_YourModelName(); // DB table
    $info = $this->your_model_name->info();
    $dat = explode("','", preg_replace("/(enum\('|'\))/", "", $info['metadata']['your_field_name']['DATA_TYPE']));
    return $dat;
}

A method for your application model mapper. Assuming your_field_name is the name of the database column that you need to list the ENUM values of.

The method returns an array with the ENUM values of your_field_name column.

public function getAvailableEnumTypes() {
    $this->your_model_name = new Application_Model_DbTable_YourModelName(); // DB table
    $info = $this->your_model_name->info();
    $dat = explode("','", preg_replace("/(enum\('|'\))/", "", $info['metadata']['your_field_name']['DATA_TYPE']));
    return $dat;
}
彼岸花ソ最美的依靠 2024-10-08 06:20:47

我用下面的方式做到了:

$metadata = $this->info(self::METADATA);
$typesString = $metadata['enum_column_name']['DATA_TYPE'];
preg_match("=\((.*)\)=is", $typesString, $parts);
$enumColumnValues = explode("','", trim($parts[1], "'"));

I did it in next way:

$metadata = $this->info(self::METADATA);
$typesString = $metadata['enum_column_name']['DATA_TYPE'];
preg_match("=\((.*)\)=is", $typesString, $parts);
$enumColumnValues = explode("','", trim($parts[1], "'"));
廻憶裏菂餘溫 2024-10-08 06:20:47

我就是这样做的:

在你的模型中放入这个

function getInfoTabella()
    {
        $data = $this->info(self::METADATA);
        return $data;
    }

然后使用这个:

$model = new $model_name();
        $description = $model->getInfoTabella();
        $enum = $description[$FIELD_NAME]['DATA_TYPE'];

        $inizia_enum = strpos($enum, "'");
        $finisce_enum = strrpos($enum, "'");
        if ($inizia_enum === false || $finisce_enum  === false)
            throw new Exception('errore enum database');

        $finisce_enum -= $inizia_enum ;


        $enum = substr($enum, $inizia_enum, $finisce_enum+1);
        str_replace("'", '', $enum);
        $enum = explode("," , $enum);
return $enum;

This is how i did it:

in your model put this

function getInfoTabella()
    {
        $data = $this->info(self::METADATA);
        return $data;
    }

then use this:

$model = new $model_name();
        $description = $model->getInfoTabella();
        $enum = $description[$FIELD_NAME]['DATA_TYPE'];

        $inizia_enum = strpos($enum, "'");
        $finisce_enum = strrpos($enum, "'");
        if ($inizia_enum === false || $finisce_enum  === false)
            throw new Exception('errore enum database');

        $finisce_enum -= $inizia_enum ;


        $enum = substr($enum, $inizia_enum, $finisce_enum+1);
        str_replace("'", '', $enum);
        $enum = explode("," , $enum);
return $enum;
停顿的约定 2024-10-08 06:20:47

这是从 MySQL 获取爆炸就绪字符串的方法:

SELECT REPLACE(TRIM(TRAILING ')' FROM (TRIM(LEADING 'enum(' FROM c.COLUMN_TYPE))), '\'', '')
FROM information_schema.`COLUMNS` c
WHERE c.COLUMN_NAME = 'enum_col'

您只需对其执行 explode(',' $result) 即可获取包含枚举值的数组。

请记住,您需要对 information_schema-database 进行读取访问才能执行此操作。

This is how you can get an explode-ready string from MySQL:

SELECT REPLACE(TRIM(TRAILING ')' FROM (TRIM(LEADING 'enum(' FROM c.COLUMN_TYPE))), '\'', '')
FROM information_schema.`COLUMNS` c
WHERE c.COLUMN_NAME = 'enum_col'

You just need to do a explode(',' $result) on it to get an array with your enum-values.

Remember that you need read-access to information_schema-database to do this.

情绪少女 2024-10-08 06:20:47

将其添加到您的 Zend_Table 类中:

$adapter = $this->getDefaultAdapter();
$sql     = 'SHOW COLUMNS FROM `table` LIKE `field`';
$result  = $adapter->fetchRow($sql);

preg_match('=\((.*)\)=is' $options);
str_replace("'", '' $options[1]);
$options = explode(',', $options[1]);

Add this in your Zend_Table class:

$adapter = $this->getDefaultAdapter();
$sql     = 'SHOW COLUMNS FROM `table` LIKE `field`';
$result  = $adapter->fetchRow($sql);

preg_match('=\((.*)\)=is' $options);
str_replace("'", '' $options[1]);
$options = explode(',', $options[1]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文