Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Fix odoo schema float type
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Mar 9, 2019
1 parent b81006a commit fe85302
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ public function getTableSchema(string $tableName = null)
}

/**
* @param mixed|null $tableName
* @return array
*/
public function findUniqueIndexes()
public function findUniqueIndexes($tableName): array
{
return [['id']];
return $tableName ? [['id']] : [['id']];
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/gii/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use panlatent\odoo\ActiveRecord;
use panlatent\odoo\Connection;
use Yii;
use yii\db\Schema;
use yii\helpers\Inflector;

/**
Expand Down Expand Up @@ -181,4 +182,69 @@ protected function generateRelations()

return $relations;
}

/**
* @inheritdoc
*/
public function generateRules($table)
{
if ($this->generateRelations !== self::RELATIONS_NONE) {
return parent::generateRules($table);
}

$types = [];
$lengths = [];

foreach ($table->columns as $column) {
if ($column->autoIncrement) {
continue;
}
if (!$column->allowNull && $column->defaultValue === null) {
$types['required'][] = $column->name;
}
switch ($column->type) {
case Schema::TYPE_SMALLINT:
case Schema::TYPE_INTEGER:
case Schema::TYPE_BIGINT:
case Schema::TYPE_TINYINT:
$types['integer'][] = $column->name;
break;
case Schema::TYPE_BOOLEAN:
$types['boolean'][] = $column->name;
break;
case Schema::TYPE_FLOAT:
case Schema::TYPE_DOUBLE:
case Schema::TYPE_DECIMAL:
case Schema::TYPE_MONEY:
$types['number'][] = $column->name;
break;
case Schema::TYPE_DATE:
case Schema::TYPE_TIME:
case Schema::TYPE_DATETIME:
case Schema::TYPE_TIMESTAMP:
case Schema::TYPE_JSON:
$types['safe'][] = $column->name;
break;
default: // strings
if ($column->size > 0) {
$lengths[$column->size][] = $column->name;
} else {
$types['string'][] = $column->name;
}
}
}
$rules = [];
$driverName = $this->getDbDriverName();
foreach ($types as $type => $columns) {
if ($driverName === 'pgsql' && $type === 'integer') {
$rules[] = "[['" . implode("', '", $columns) . "'], 'default', 'value' => null]";
}
$rules[] = "[['" . implode("', '", $columns) . "'], '$type']";
}
foreach ($lengths as $length => $columns) {
$rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]";
}

return $rules;
}
}
2 changes: 2 additions & 0 deletions src/helpers/FieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function type2ColumnType(string $type): string
case 'date':
case 'time':
case 'datetime':
case 'float':
case 'binary':
return $type;
}
Expand Down Expand Up @@ -69,6 +70,7 @@ public static function type2PhpType(string $type): string
case 'many2many':
return 'array';
case 'boolean':
case 'float':
case 'integer':
return $type;
}
Expand Down

0 comments on commit fe85302

Please sign in to comment.