![]() |
|
Snippets |
|
Problem:
Mysql produce TINYINT fields because BOOLEAN is just a synonym for TINYINT. And if you have TINYINT fields with value 0 or 1 (like BOOLEAN fields) in your database and use command symfony propel-build-schema to generate schema, you will have one problem with admin generator, because it will produse TINYINT fields as text form fields with value - 0 or 1, not as checkboxes. Admin generator needs BOOLEAN fields in schema to produse checkboxes.
Solution:
Solution to automate transformation field type tinyint to boolean where field name prefix is "is_".
sfPakeTransformTinyint.php
<?php pake_desc( 'apply tinyint-boolean transformation to your data model' ); pake_task( 'transform-schema-tinyint', 'project_exists' ); function run_transform_schema_tinyint( $task, $args ) { // Check params // -- missing params ? if ( !count($args) ) { throw new Exception( 'You must provide a transformation to apply.' ); } // -- schema exists ? $schema_filename = sprintf( '%s/schema.xml', sfConfig::get('sf_config_dir') ); if ( !file_exists($schema_filename) ) { throw new Exception( "Missing schema.xml" ); } // Backup schema pake_copy( $schema_filename, $schema_filename . '.previous', array('override' => true) ); //do hard work - tinyint->boolean if ($args[0] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); $contents = preg_replace('/(name="is_.*?type=")TINYINT"/i','$1BOOLEAN"',$contents); $handle = fopen($schema_filename, "w+"); fwrite($handle, $contents); fclose($handle); } //undo hard work - boolean->tinyint if ($args[0] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); $contents = preg_replace('/(name="is_.*?type=")BOOLEAN"/i','$1TINYINT"',$contents); $handle = fopen($schema_filename, "w+"); fwrite($handle, $contents); fclose($handle); } } ?>
copy this code and drop it as new file in SF_DATA_DIR/tasks/ use command: symfony transform-schema-tinyint do to change tinyint to boolean, where field name with "is_" prefix, than rebuild your model with propel-build-model, clear cache, and use admin generator with checkboxes.
If something going wrong, do this command to undo changes in your model:
symfony transform-schema-tinyint undo (to change boolean to tinyint, where field name with "is_" prefix)
or you may use schema.xml.previous <- this is your schema before transformation
Comments on this snippet
I've modified this to work with the YML format...
<?php pake_desc( 'apply tinyint-boolean transformation to your data model' ); pake_task( 'transform-schema-tinyint', 'project_exists' );
function run_transform_schema_tinyint( $task, $args ) {
}
?>
well.. that didnt turn out so nice! here it is again... in code tags hopefully!
Hi,
Rename the file sfPakeTransformTinyint.php to myPakeTransformTinyint.php to put in SF_DATA_DIR/tasks/ directory to work properly. Now you can use the command: symfony transform-schema-tinyint do
Olivier
I've merge the previous files in one file and enhance the YML conversion There is a new argument "xml" symfony transform-schema-tinyint do -> to convert schema.yml symfony transform-schema-tinyint xml do -> to convert schema.xml
<?php pake_desc( 'apply tinyint-boolean transformation to your data model' ); pake_task( 'transform-schema-tinyint', 'project_exists' );
function run_transform_schema_tinyint($task, $args) { // Check params // -- missing params ? if ( !count($args) ) { throw new Exception("You must provide a transformation to apply.\nsymfony transform-schema-tinyint do\nsymfony transform-schema-tinyint xml do\nsymfony transform-schema-tinyint undo\nsymfony transform-schema-tinyint xml undo"); }
// -- schema exists ? if ($args[0] == 'xml') { $schema_filename = sprintf( '%s/schema.xml', sfConfig::get('sf_config_dir') ); if ( !file_exists($schema_filename) ) { throw new Exception( "Missing schema.xml" ); } } else { $schema_filename = sprintf( '%s/schema.yml', sfConfig::get('sf_config_dir') ); if ( !file_exists($schema_filename) ) { throw new Exception( "Missing schema.yml" ); } }
// Backup schema pake_copy( $schema_filename, $schema_filename . '.previous', array('override' => true) );
// do hard work - tinyint->boolean if ($args[0] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $line = fgets($handle); $contents.= $line;
}
if ($args[0] == 'xml' && $args[1] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);
}
//undo hard work - boolean->tinyint if ($args[0] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $line = fgets($handle); $contents.= $line;
}
if ($args[0] == 'xml' && $args[1] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);
} } ?>
Again !
I've merge the previous files in one file and enhance the YML conversion. There is a new argument "xml" * symfony transform-schema-tinyint do -> to convert schema.yml * symfony transform-schema-tinyint xml do -> to convert schema.xml
file:SF_DATA_DIR/tasks/myPakeTransformTinyint.php {{{ <?php pake_desc( 'apply tinyint-boolean transformation to your data model' ); pake_task( 'transform-schema-tinyint', 'project_exists' );
function run_transform_schema_tinyint($task, $args) { // Check params // -- missing params ? if ( !count($args) ) { throw new Exception("You must provide a transformation to apply.\nsymfony transform-schema-tinyint do\nsymfony transform-schema-tinyint xml do\nsymfony transform-schema-tinyint undo\nsymfony transform-schema-tinyint xml undo"); }
// -- schema exists ? if ($args[0] == 'xml') { $schema_filename = sprintf( '%s/schema.xml', sfConfig::get('sf_config_dir') ); if ( !file_exists($schema_filename) ) { throw new Exception( "Missing schema.xml" ); } } else { $schema_filename = sprintf( '%s/schema.yml', sfConfig::get('sf_config_dir') ); if ( !file_exists($schema_filename) ) { throw new Exception( "Missing schema.yml" ); } }
// Backup schema pake_copy( $schema_filename, $schema_filename . '.previous', array('override' => true) );
// do hard work - tinyint->boolean if ($args[0] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $line = fgets($handle); $contents.= $line;
}
if ($args[0] == 'xml' && $args[1] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);
}
//undo hard work - boolean->tinyint if ($args[0] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $line = fgets($handle); $contents.= $line;
}
if ($args[0] == 'xml' && $args[1] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);
} } ?> }}}