Changeset 5519
- Timestamp:
- 10/15/07 11:17:53 (1 year ago)
- Files:
-
- plugins/sfPropelAlternativeSchemaPlugin/README (modified) (4 diffs)
- plugins/sfPropelAlternativeSchemaPlugin/lib/sfPropelDatabaseSchema.class.php (modified) (5 diffs)
- plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/new_schema.yml (modified) (1 diff)
- plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/schema.xml (modified) (1 diff)
- plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/schema.yml (modified) (1 diff)
- plugins/sfPropelAlternativeSchemaPlugin/test/unit/sfPropelDatabaseSchemaTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelAlternativeSchemaPlugin/README
r5415 r5519 110 110 stripped_title: { type: longvarchar, required: true, primaryKey: true, sequence: my_custom_sequence_name } 111 111 user_id: 112 my_group: { type: integer, foreign Table: ab_group, foreignReference: id, onDelete: setnull }112 my_group: { type: integer, foreignClass: Group, foreignReference: id, onDelete: setnull } 113 113 created_at: timestamp 114 114 updated_at: … … 141 141 142 142 The `connection` parameter is optional. If it is not set, it will take `propel` as a default value. 143 144 Note 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. 143 145 144 146 Last but not least, all the 'magic' of the current syntax is still there (auto definition of primary keys, foreign keys, i18n tables, etc.). … … 251 253 }}} 252 254 255 == Checking that the plugin is installed == 256 257 Alternative 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 259 To 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 253 265 == TODO == 254 266 … … 257 269 == Changelog == 258 270 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 259 277 === 2007-10-05 | 0.9.0 Beta === 260 278 plugins/sfPropelAlternativeSchemaPlugin/lib/sfPropelDatabaseSchema.class.php
r5415 r5519 140 140 { 141 141 $tableName = $classParams['tableName']; 142 if($tableName != sfInflector::underscore($className))143 {144 $tableAttributes['phpName'] = $className;145 }146 142 unset($classParams['tableName']); 147 143 } … … 150 146 $tableName = sfInflector::underscore($className); 151 147 } 148 149 if(sfInflector::camelize($tableName) != $className) 150 { 151 $tableAttributes['phpName'] = $className; 152 } 153 152 154 if($tableAttributes || $classParams) 153 155 { … … 435 437 foreach ($this->getTables() as $tb_name => $table) 436 438 { 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 ) 438 448 { 439 449 $table_match = $tb_name; 450 break; 440 451 } 441 452 } … … 451 462 foreach ($column as $key => $value) 452 463 { 453 if (!in_array($key, array('foreign Table', 'foreignReference', 'onDelete', 'onUpdate', 'index', 'unique', 'sequence')))464 if (!in_array($key, array('foreignClass', 'foreignTable', 'foreignReference', 'onDelete', 'onUpdate', 'index', 'unique', 'sequence'))) 454 465 { 455 466 $attributes_string .= " $key=\"".htmlspecialchars($this->getCorrectValueFor($key, $value))."\""; … … 464 475 465 476 // 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 469 496 if (isset($column['onDelete'])) 470 497 { plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/new_schema.yml
r5415 r5519 33 33 user_id: 34 34 my_group: { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 35 my_other_group: { type: integer, foreignClass: Group, foreignReference: id, onDelete: setnull } 35 36 created_at: timestamp 36 37 updated_at: plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/schema.xml
r5415 r5519 46 46 <reference local="my_group" foreign="id" /> 47 47 </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> 48 52 <column name="created_at" type="timestamp" /> 49 53 <column name="updated_at" type="timestamp" /> plugins/sfPropelAlternativeSchemaPlugin/test/unit/fixtures/schema.yml
r5415 r5519 22 22 user_id: 23 23 my_group: { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 24 my_other_group: { type: integer, foreignTable: ab_group, foreignReference: id, onDelete: setnull } 24 25 created_at: timestamp 25 26 updated_at: plugins/sfPropelAlternativeSchemaPlugin/test/unit/sfPropelDatabaseSchemaTest.php
r5415 r5519 41 41 } 42 42 43 $t = new my_lime_test(1 45, new lime_output_color());43 $t = new my_lime_test(153, new lime_output_color()); 44 44 45 45 require_once(dirname(__FILE__).'/../../lib/sfPropelDatabaseSchema.class.php');