mirror of
https://github.com/processwire/processwire.git
synced 2025-08-09 08:17:12 +02:00
Update FieldtypeFloat to support configurable column type of either 'float' or 'double'. Previously it only supported 'float' unless you manually changed the DB to use double.
This commit is contained in:
@@ -19,7 +19,7 @@ class FieldtypeFloat extends Fieldtype {
|
|||||||
return array(
|
return array(
|
||||||
'title' => __('Float', __FILE__),
|
'title' => __('Float', __FILE__),
|
||||||
'summary' => __('Field that stores a floating point number', __FILE__),
|
'summary' => __('Field that stores a floating point number', __FILE__),
|
||||||
'version' => 107,
|
'version' => 108,
|
||||||
'permanent' => true,
|
'permanent' => true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -175,10 +175,56 @@ class FieldtypeFloat extends Fieldtype {
|
|||||||
*/
|
*/
|
||||||
public function getDatabaseSchema(Field $field) {
|
public function getDatabaseSchema(Field $field) {
|
||||||
$schema = parent::getDatabaseSchema($field);
|
$schema = parent::getDatabaseSchema($field);
|
||||||
$schema['data'] = 'float NOT NULL';
|
$schema['data'] = ($field->get('colType') === 'double' ? 'double' : 'float') . ' NOT NULL';
|
||||||
return $schema;
|
return $schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the database data column type (updating the DB table)
|
||||||
|
*
|
||||||
|
* @param Field $field
|
||||||
|
* @param string $newType Specify either 'float' or 'double'
|
||||||
|
* @return string Returns the colum
|
||||||
|
* @since 3.0.241
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function setColumnType(Field $field, $newType) {
|
||||||
|
$oldType = $this->getColumnType($field);
|
||||||
|
if($newType != 'double') $newType = $this->getDefaultColumnType();
|
||||||
|
if($oldType === $newType) return $oldType;
|
||||||
|
$database = $this->wire()->database;
|
||||||
|
$table = $field->getTable();
|
||||||
|
$database->exec("ALTER TABLE $table MODIFY `data` $newType NOT NULL");
|
||||||
|
$field->warning("Changed '$field->name' type from '$oldType' to '$newType'", Notice::debug);
|
||||||
|
$field->set('colType', $newType);
|
||||||
|
return $newType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current database data column type
|
||||||
|
*
|
||||||
|
* @param Field $field
|
||||||
|
* @return string
|
||||||
|
* @since 3.0.241
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function getColumnType(Field $field) {
|
||||||
|
$database = $this->wire()->database;
|
||||||
|
$table = $field->getTable();
|
||||||
|
try {
|
||||||
|
$cols = $database->getColumns($table, true);
|
||||||
|
$type = $cols['data']['type'];
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
$this->error($e->getMessage());
|
||||||
|
$type = $this->getDefaultColumnType();
|
||||||
|
}
|
||||||
|
return $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getDefaultColumnType() {
|
||||||
|
return 'float';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get field configuration
|
* Get field configuration
|
||||||
*
|
*
|
||||||
@@ -191,8 +237,20 @@ class FieldtypeFloat extends Fieldtype {
|
|||||||
$precision = $field->get('precision');
|
$precision = $field->get('precision');
|
||||||
if($precision === null) $precision = 2;
|
if($precision === null) $precision = 2;
|
||||||
|
|
||||||
/** @var InputfieldInteger $f */
|
$colType = strtolower($this->getColumnType($field));
|
||||||
$f = $this->wire()->modules->get('InputfieldInteger');
|
$newType = $field->get('colType');
|
||||||
|
if(empty($newType)) $newType = $colType;
|
||||||
|
if($newType != $colType) $colType = $this->setColumnType($field, $newType);
|
||||||
|
|
||||||
|
$f = $inputfields->InputfieldRadios;
|
||||||
|
$f->attr('name', 'colType');
|
||||||
|
$f->label = $this->_('Database column type');
|
||||||
|
$f->addOption('float', $this->_('Float (single-precision)'));
|
||||||
|
$f->addOption('double', $this->_('Double (double-precision)'));
|
||||||
|
$f->val($colType);
|
||||||
|
$inputfields->add($f);
|
||||||
|
|
||||||
|
$f = $inputfields->InputfieldInteger;
|
||||||
$f->attr('name', 'precision');
|
$f->attr('name', 'precision');
|
||||||
$f->label = $this->_('Number of decimal digits to round to');
|
$f->label = $this->_('Number of decimal digits to round to');
|
||||||
$f->description = $this->_('Or use a negative number like `-1` to disable rounding.');
|
$f->description = $this->_('Or use a negative number like `-1` to disable rounding.');
|
||||||
|
Reference in New Issue
Block a user