Fixed xf-dev:generate-schema-entity doesn't handle boolean default values

DragonByte Tech

Well-known member
Affected version
2.0.9
Consider this line in an entity: 'has_demo' => ['type' => self::BOOL, 'default' => false],
This will generate the following line in the SchemaManager output: $table->addColumn('has_demo', 'tinyint')->setDefault();

Consider this line in an entity: 'active' => ['type' => self::BOOL, 'default' => true],
This will generate the following line in the SchemaManager output: $table->addColumn('active', 'tinyint')->setDefault(0);


Fillip
 
I've noticed this behavior as well. In the core, the structure defaults seem to be set as 1 or 0 in a lot of places rather than true or false. It would be nice if the tool supported booleans as well.
 
Diff:
Index: src/XF/Cli/Command/Development/GenerateSchemaEntity.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/XF/Cli/Command/Development/GenerateSchemaEntity.php    (revision 68d003876d3d6328f1c9be21923775dbc074e96a)
+++ src/XF/Cli/Command/Development/GenerateSchemaEntity.php    (date 1536226388000)
@@ -112,6 +112,10 @@
                 {
                     $default = '\'' . $column['default'] . '\'';
                 }
+                else if (is_bool($column['default']))
+                {
+                    $default = ($column['default'] === true) ? 'true' : 'false';
+                }
                 else
                 {
                     $default = $column['default'];
I haven't tested this, but I asssume it works?
 
In my testing, I'm finding that all columns that are ['type' => self::BOOL, 'default' => true] will hit this condition:
PHP:
if ($column['default'] == \XF::$time)
{
    \XF::dumpSimple($column['default']);
    $default = 0;
}
This is the output when I run the generator using that code:
bool(true)
int(1536226655)
int(1536226655)
int(1536226655)
bool(true)
bool(true)
bool(true)

For that reason, changing the relevant block to this:
PHP:
                if (is_bool($column['default']))
                {
                    $default = ($column['default'] === true) ? 1 : 0;
                }
                else if ($column['default'] == \XF::$time)
                {
                    $default = 0;
                }
Is needed to have the desired effect.

By the way, .patch is not an allowed extension here for some reason, hopefully it still works when I just change the extension :P


Fillip
 

Attachments

This is the correct fix:
Diff:
Index: src/XF/Cli/Command/Development/GenerateSchemaEntity.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/XF/Cli/Command/Development/GenerateSchemaEntity.php    (revision 68d003876d3d6328f1c9be21923775dbc074e96a)
+++ src/XF/Cli/Command/Development/GenerateSchemaEntity.php    (date 1536227229000)
@@ -104,7 +104,7 @@

             if (isset($column['default']) && $allowedDefault)
             {
-                if ($column['default'] == \XF::$time)
+                if ($column['default'] === \XF::$time)
                 {
                     $default = 0;
                 }
@@ -112,6 +112,10 @@
                 {
                     $default = '\'' . $column['default'] . '\'';
                 }
+                else if (is_bool($column['default']))
+                {
+                    $default = ($column['default'] === true) ? 1 : 0;
+                }
                 else
                 {
                     $default = $column['default'];
 
Back
Top Bottom