![]() |
|
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