Как сохранить значения массива в таблицу? - PullRequest
0 голосов
/ 05 сентября 2011

У меня есть эти коды в моем представлении добавления

$status = array('0' => 'Resolved', '1' => 'Assigned/Unresolved', '2' => 'Suspended', '3' => 'Closed', '4' => 'Bypassed');
echo $this->Form->input('status', array('options' => $status));

и вместо сохранения значения (например, Resolved) в таблицу сохраняет индекс массива. Есть идеи?

Ответы [ 2 ]

1 голос
/ 06 сентября 2011

Разве вы не можете определить индекс как желаемое значение?

$status = array('Resolved' => 'Resolved', 'Assigned/Unresolved' => 'Assigned/Unresolved', etc etc );
    echo $this->Form->input('status', array('options' => $status));
1 голос
/ 06 сентября 2011

Вы должны сделать что-то вроде этого:

$status = array('resolved' => 'Resolved', 'assigned' => 'Assigned/Unresolved'...);

Сохраняет индекс массива. Но это не очень хорошая практика, вместо этого попробуйте использовать перечисления. Проверьте это:

/**
     * Get Enum Values
     * Snippet v0.1.3
     * http://cakeforge.org/snippet/detail.php?type=snippet&id=112
     *
     * Gets the enum values for MySQL 4 and 5 to use in selectTag()
     */
    function getEnumValues($columnName=null, $respectDefault=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] = Inflector::humanize($value);
                    }
        }
        else{
                    $assoc_values = array();
                    foreach ( $values as $value ) {
                                    $assoc_values[$value] = Inflector::humanize($value);
                    }
        }

        return $assoc_values;
    } //end getEnumValues

Вставьте этот метод в вашу AppModel.

Создайте столбец в своей таблице как перечисление с возможными значениями и используйте этот метод для их получения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...