Development

Changeset 5519

You must first sign up to be able to contribute.

Changeset 5519

Show
Ignore:
Timestamp:
10/15/07 11:17:53 (1 year ago)
Author:
francois
Message:

sfPropelAlternativeSchemaPlugin Added a new foreignClass column attribute, Fixed a problem with model class names beginning with a lowercase character

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelAlternativeSchemaPlugin/README

    r5415 r5519  
    110110      stripped_title: { type: longvarchar, required: true, primaryKey: true, sequence: my_custom_sequence_name } 
    111111      user_id: 
    112       my_group:       { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 
     112      my_group:       { type: integer, foreignClass: Group, foreignReference: id, onDelete: setnull } 
    113113      created_at:     timestamp 
    114114      updated_at: 
     
    141141 
    142142The `connection` parameter is optional. If it is not set, it will take `propel` as a default value. 
     143 
     144Note that you can define foreign keys either with the usual `foreignTable` attribute, which expects a table name, or via the new `foreignClass` attribute, which expects a class name. 
    143145 
    144146Last but not least, all the 'magic' of the current syntax is still there (auto definition of primary keys, foreign keys, i18n tables, etc.). 
     
    251253}}} 
    252254 
     255== Checking that the plugin is installed == 
     256 
     257Alternative schemas can sometimes look like normal YAML schemas. If this plugin is not installed, the usual schema interpreter may try to transform a schema with the alternative syntax into an XML schema, but based on the usual syntax. This will most probably cause problems. 
     258 
     259To avoid this, you should check that the plugin is installed before trying to interpret an alternative YAML schema. To do so, a good trick is to take advantage of the fact that YAML files are executed as PHP files before being converted to arrays. So add the following line on top of every alternative YAML schema: 
     260 
     261{{{ 
     262<?php if(!is_callable(array('sfPropelDatabaseSchema', 'loadNewYaml'))) throw new Exception('You must install the sfPropelAlternativeSchemaPlugin to use this schema') ?> 
     263}}} 
     264 
    253265== TODO == 
    254266 
     
    257269== Changelog == 
    258270 
     271=== Trunk === 
     272 
     273 * francois: Added a new `foreignClass` column attribute to define a foreign key from a phpName rather than from a tableName 
     274 * francois: Added section about plugin check in README 
     275 * francois: Fixed a problem with model class names beginning with a lowercase character 
     276 
    259277=== 2007-10-05 | 0.9.0 Beta ===  
    260278 
  • plugins/sfPropelAlternativeSchemaPlugin/lib/sfPropelDatabaseSchema.class.php

    r5415 r5519  
    140140        { 
    141141          $tableName = $classParams['tableName']; 
    142           if($tableName != sfInflector::underscore($className)) 
    143           { 
    144             $tableAttributes['phpName'] = $className; 
    145           } 
    146142          unset($classParams['tableName']); 
    147143        } 
     
    150146          $tableName = sfInflector::underscore($className); 
    151147        } 
     148         
     149        if(sfInflector::camelize($tableName) != $className) 
     150        { 
     151          $tableAttributes['phpName'] = $className; 
     152        } 
     153         
    152154        if($tableAttributes || $classParams) 
    153155        { 
     
    435437    foreach ($this->getTables() as $tb_name => $table) 
    436438    { 
    437       if ((isset($table['_attributes']['phpName']) && $table['_attributes']['phpName'] == sfInflector::camelize($table_name)) || ($tb_name == $table_name)) 
     439      if ( 
     440           ($tb_name == $table_name) 
     441           || (isset($table['_attributes']['phpName']) &&  
     442             ( 
     443               $table['_attributes']['phpName'] == sfInflector::camelize($table_name)  
     444               || $table['_attributes']['phpName'] == $table_name 
     445             ) 
     446           || (sfInflector::underscore($table_name) == $tb_name))  
     447         ) 
    438448      { 
    439449        $table_match = $tb_name; 
     450        break; 
    440451      } 
    441452    } 
     
    451462      foreach ($column as $key => $value) 
    452463      { 
    453         if (!in_array($key, array('foreignTable', 'foreignReference', 'onDelete', 'onUpdate', 'index', 'unique', 'sequence'))) 
     464        if (!in_array($key, array('foreignClass', 'foreignTable', 'foreignReference', 'onDelete', 'onUpdate', 'index', 'unique', 'sequence'))) 
    454465        { 
    455466          $attributes_string .= " $key=\"".htmlspecialchars($this->getCorrectValueFor($key, $value))."\""; 
     
    464475 
    465476    // conventions for foreign key attributes 
    466     if (is_array($column) && isset($column['foreignTable'])) 
    467     { 
    468       $attributes_string .= "    <foreign-key foreignTable=\"$column[foreignTable]\""; 
     477    if (is_array($column) && (isset($column['foreignTable']) || isset($column['foreignClass']))) 
     478    { 
     479      if (isset($column['foreignTable'])) 
     480      { 
     481        $attributes_string .= "    <foreign-key foreignTable=\"$column[foreignTable]\""; 
     482      } 
     483      else 
     484      { 
     485        $foreignTable = $this->findTable($column['foreignClass']); 
     486        if(!$foreignTable) 
     487        { 
     488          // Let's assume that the class given is from another schema 
     489          // We have no access to the other schema's phpNames 
     490          // So our last guess is to try to underscore the class name 
     491          $foreignTable = sfInflector::underscore($column['foreignClass']); 
     492        } 
     493        $attributes_string .= "    <foreign-key foreignTable=\"".$foreignTable."\""; 
     494      } 
     495       
    469496      if (isset($column['onDelete'])) 
    470497      { 
  • plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/new_schema.yml

    r5415 r5519  
    3333      user_id: 
    3434      my_group:       { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 
     35      my_other_group: { type: integer, foreignClass: Group, foreignReference: id, onDelete: setnull } 
    3536      created_at:     timestamp 
    3637      updated_at: 
  • plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/schema.xml

    r5415 r5519  
    4646      <reference local="my_group" foreign="id" /> 
    4747    </foreign-key> 
     48    <column name="my_other_group" type="integer" /> 
     49    <foreign-key foreignTable="ab_group" onDelete="setnull"> 
     50      <reference local="my_other_group" foreign="id" /> 
     51    </foreign-key> 
    4852    <column name="created_at" type="timestamp" /> 
    4953    <column name="updated_at" type="timestamp" /> 
  • plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/schema.yml

    r5415 r5519  
    2222    user_id: 
    2323    my_group:       { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 
     24    my_other_group: { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 
    2425    created_at:     timestamp 
    2526    updated_at: 
  • plugins/sfPropelAlternativeSchemaPlugin/test/unit/sfPropelDatabaseSchemaTest.php

    r5415 r5519  
    4141} 
    4242 
    43 $t = new my_lime_test(145, new lime_output_color()); 
     43$t = new my_lime_test(153, new lime_output_color()); 
    4444 
    4545require_once(dirname(__FILE__).'/../../lib/sfPropelDatabaseSchema.class.php');