We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Get available values for "enum" field

I have MySQL table with a field of "enum" type. How can I get available values for this field through Model or ModelsMetaData?

edited Mar '14

I use the following method in Zend Framework DbTable models, you can port it to Phalcon php models easily

<?php
    /**
     * build select to read column definition for selected column
     * @param string $column
     * @return array
     */
    public function getFieldOptions($column)
    {
        $sql  = " SHOW COLUMNS ";
        $sql .= " FROM " . $this->_name;
        $sql .= " LIKE '" . $column . "'";

        $result = $this->getAdapter()->fetchRow($sql);

        if (!is_array($result)) {
            $result = (array) $result;
        }
        // user regular expression to get option list from result
        preg_match("=\((.*)\)=is", $result["Type"], $options);

        // replace single quotes
        $options = str_replace("'", "", $options[1]);

        // explode string into an array
        $options = explode(",", $options);

        return $options;
    }