Code snippets for symfony 1.x

Navigation

sfPakeTransformTinyint.php

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

by Alex Gemini on 2006-08-28, tagged cli  generator  model  pake  task 

Comments on this snippet

gravatar icon
#1 Daniel Graetzer on 2007-10-17 at 01:24

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 ) {

// Check params
// -- missing params ?
if ( !count($args) ) {
    throw new Exception( &#039;You must provide a transformation to apply.&#039; );
}
 
// -- schema exists ?
$schema_filename = sprintf( &#039;%s/schema.yml&#039;, sfConfig::get(&#039;sf_config_dir&#039;) );
if ( !file_exists($schema_filename) ) {
    throw new Exception( &quot;Missing schema.yml&quot; );
}
 
// Backup schema
pake_copy( $schema_filename, $schema_filename . &#039;.previous&#039;, array(&#039;override&#039; =&gt; true) );
 
 //do hard work - tinyint-&gt;boolean
if ($args[0] == &#039;do&#039;) {
    $handle = fopen($schema_filename, &quot;r&quot;);
    $contents = &#039;&#039;;
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
 
    $contents = preg_replace(&#039;/TINYINT/i&#039;,&#039;BOOLEAN&#039;,$contents);
 
    $handle = fopen($schema_filename, &quot;w+&quot;);
    fwrite($handle, $contents);
    fclose($handle);
}
 //undo hard work - boolean-&gt;tinyint
if ($args[0] == &#039;undo&#039;) {
    $handle = fopen($schema_filename, &quot;r&quot;);
    $contents = &#039;&#039;;
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
 
    $contents = preg_replace(&#039;/BOOLEAN/i&#039;,&#039;TINYINT&#039;,$contents);
 
    $handle = fopen($schema_filename, &quot;w+&quot;);
    fwrite($handle, $contents);
    fclose($handle);
}
 

}

?>

gravatar icon
#2 Daniel Graetzer on 2007-10-17 at 01:27

well.. that didnt turn out so nice! here it is again... in code tags hopefully!

<?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.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)) {
          $contents .= fread($handle, 8192);
        }
        fclose($handle);
 
        $contents = preg_replace('/TINYINT/i','BOOLEAN',$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('/BOOLEAN/i','TINYINT',$contents);
 
        $handle = fopen($schema_filename, "w+");
        fwrite($handle, $contents);
        fclose($handle);
    }
 
}
 
?>
 
gravatar icon
#3 Olivier LOYNET on 2008-02-10 at 08:47

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

gravatar icon
#4 Olivier LOYNET on 2008-02-10 at 11:10

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 (preg_match(&#039;/( is_.*)/i&#039;, $line))
  {
    $line = fgets($handle);
    $contents.= preg_replace(&#039;/TINYINT/i&#039;, &#039;BOOLEAN&#039;, $line);
  }
}
fclose($handle);
 
$handle = fopen($schema_filename, &quot;w+&quot;);
fwrite($handle, $contents);
fclose($handle);
 

}

if ($args[0] == 'xml' && $args[1] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);

$contents = preg_replace(&#039;/(name=&quot;is_.*?type=&quot;)TINYINT&quot;/i&#039;,&#039;$1BOOLEAN&quot;&#039;,$contents);
 
$handle = fopen($schema_filename, &quot;w+&quot;);
fwrite($handle, $contents);
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 (preg_match(&#039;/( is_.*)/i&#039;, $line))
    {
      $line = fgets($handle);
      $contents.= preg_replace(&#039;/BOOLEAN/i&#039;,&#039;TINYINT&#039;,$line);
    }
  }
  fclose($handle);
 
  $handle = fopen($schema_filename, &quot;w+&quot;);
  fwrite($handle, $contents);
  fclose($handle);
 

}

if ($args[0] == 'xml' && $args[1] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);

$contents = preg_replace(&#039;/(name=&quot;is_.*?type=&quot;)BOOLEAN&quot;/i&#039;,&#039;$1TINYINT&quot;&#039;,$contents);
 
$handle = fopen($schema_filename, &quot;w+&quot;);
fwrite($handle, $contents);
fclose($handle);
 

} } ?>

gravatar icon
#5 Olivier LOYNET on 2008-02-20 at 07:29

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 (preg_match(&#039;/( is_.*)/i&#039;, $line))
  {
    $line = fgets($handle);
    $contents.= preg_replace(&#039;/TINYINT/i&#039;, &#039;BOOLEAN&#039;, $line);
  }
}
fclose($handle);
 
$handle = fopen($schema_filename, &quot;w+&quot;);
fwrite($handle, $contents);
fclose($handle);
 

}

if ($args[0] == 'xml' && $args[1] == 'do') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);

$contents = preg_replace(&#039;/(name=&quot;is_.*?type=&quot;)TINYINT&quot;/i&#039;,&#039;$1BOOLEAN&quot;&#039;,$contents);
 
$handle = fopen($schema_filename, &quot;w+&quot;);
fwrite($handle, $contents);
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 (preg_match(&#039;/( is_.*)/i&#039;, $line))
    {
      $line = fgets($handle);
      $contents.= preg_replace(&#039;/BOOLEAN/i&#039;,&#039;TINYINT&#039;,$line);
    }
  }
  fclose($handle);
 
  $handle = fopen($schema_filename, &quot;w+&quot;);
  fwrite($handle, $contents);
  fclose($handle);
 

}

if ($args[0] == 'xml' && $args[1] == 'undo') { $handle = fopen($schema_filename, "r"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);

$contents = preg_replace(&#039;/(name=&quot;is_.*?type=&quot;)BOOLEAN&quot;/i&#039;,&#039;$1TINYINT&quot;&#039;,$contents);
 
$handle = fopen($schema_filename, &quot;w+&quot;);
fwrite($handle, $contents);
fclose($handle);
 

} } ?> }}}