Changeset 8744
- Timestamp:
- 05/03/08 07:53:43 (7 months ago)
- Files:
-
- plugins/sfDoctrinePlugin/trunk/data/generator/sfDoctrineForm/default/template/sfDoctrineFormGeneratedTemplate.php (modified) (5 diffs)
- plugins/sfDoctrinePlugin/trunk/lib/generator/sfDoctrineAdminGenerator.class.php (modified) (14 diffs)
- plugins/sfDoctrinePlugin/trunk/lib/generator/sfDoctrineFormGenerator.class.php (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfDoctrinePlugin/trunk/data/generator/sfDoctrineForm/default/template/sfDoctrineFormGeneratedTemplate.php
r8726 r8744 2 2 3 3 /** 4 * <?php echo $this-> table->getOption('name')?> form base class.4 * <?php echo $this->modelName ?> form base class. 5 5 * 6 6 * @package form 7 * @subpackage <?php echo $this->underscore($this-> table->getOption('name')) ?>7 * @subpackage <?php echo $this->underscore($this->modelName) ?> 8 8 9 9 * @version SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $ 10 10 */ 11 class Base<?php echo $this-> table->getOption('name')?>Form extends BaseFormDoctrine11 class Base<?php echo $this->modelName ?>Form extends BaseFormDoctrine 12 12 { 13 13 public function setup() … … 15 15 $this->setWidgets(array( 16 16 <?php foreach ($this->table->getColumns() as $name => $column): ?> 17 '<?php echo strtolower($name) ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($name)) ?> => new <?php echo $this->getWidgetClassForColumn($ column) ?>(<?php echo $this->getWidgetOptionsForColumn($column) ?>),17 '<?php echo strtolower($name) ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($name)) ?> => new <?php echo $this->getWidgetClassForColumn($name) ?>(<?php echo $this->getWidgetOptionsForColumn($name) ?>), 18 18 <?php endforeach; ?> 19 19 <?php foreach ($this->getManyToManyRelations() as $relation): ?> … … 24 24 $this->setValidators(array( 25 25 <?php foreach ($this->table->getColumns() as $name => $column): ?> 26 '<?php echo strtolower($name) ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($name)) ?> => new <?php echo $this->getValidatorClassForColumn($ column) ?>(<?php echo $this->getValidatorOptionsForColumn($name, $column) ?>),26 '<?php echo strtolower($name) ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($name)) ?> => new <?php echo $this->getValidatorClassForColumn($name) ?>(<?php echo $this->getValidatorOptionsForColumn($name) ?>), 27 27 <?php endforeach; ?> 28 28 <?php foreach ($this->getManyToManyRelations() as $relation): ?> … … 31 31 )); 32 32 33 $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this-> table->getOption('name')) ?>[%s]');33 $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->modelName) ?>[%s]'); 34 34 35 35 $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); … … 40 40 public function getModelName() 41 41 { 42 return '<?php echo $this-> table->getOption('name')?>';42 return '<?php echo $this->modelName ?>'; 43 43 } 44 44 <?php if ($this->isI18n()): ?> plugins/sfDoctrinePlugin/trunk/lib/generator/sfDoctrineAdminGenerator.class.php
r8743 r8744 17 17 class sfDoctrineAdminGenerator extends sfAdminGenerator 18 18 { 19 /** 20 * Doctrine_Table instance for this admin generator 21 * 22 * @var Doctrine_Table $table 23 */ 19 24 protected $table; 20 25 26 /** 27 * Initialize a doctrine admin generator instance 28 * 29 * @param sfGeneratorManager $generatorManager 30 * @return void 31 */ 21 32 public function initialize(sfGeneratorManager $generatorManager) 22 33 { … … 26 37 } 27 38 39 /** 40 * Load all Doctrine_Table class to generate instance 41 * 42 * @return void 43 */ 28 44 protected function loadMapBuilderClasses() 29 45 { … … 31 47 } 32 48 49 /** 50 * Get Doctrine_Table instance for admin generator 51 * 52 * @return Doctrine_Table $table 53 */ 33 54 protected function getTable() 34 55 { … … 36 57 } 37 58 59 /** 60 * Load primary key columns as array of sfDoctrineAdminColumn instances 61 * 62 * @return void 63 * @throws sfException 64 */ 38 65 protected function loadPrimaryKeys() 39 66 { … … 55 82 } 56 83 84 /** 85 * Get columns for admin generator instance 86 * 87 * @param string $paramName 88 * @param string $category 89 * @return array $columns 90 */ 57 91 public function getColumns($paramName, $category = 'NONE') 58 92 { … … 75 109 $column->setColumnName($columnName); 76 110 77 if (isset($cols[$columnName])) // if it is not a many2many 111 // if it is not a many2many 112 if (isset($cols[$columnName])) 113 { 78 114 $column->setColumnInfo($cols[$columnName]); 115 } 79 116 80 117 $columns[$index] = $column; … … 86 123 } 87 124 125 /** 126 * Get array of all columns as sfDoctrineAdminColumn instances 127 * 128 * @return array $columns 129 */ 88 130 function getAllColumns() 89 131 { … … 95 137 // we set out to replace the foreign key to their corresponding aliases 96 138 $found = null; 97 foreach ($rels as $alias =>$rel)139 foreach ($rels as $alias => $rel) 98 140 { 99 141 $relType = $rel->getType(); 100 142 if ($rel->getLocal() == $name && $relType != Doctrine_Relation::MANY_AGGREGATE && $relType != Doctrine_Relation::MANY_COMPOSITE) 143 { 101 144 $found = $alias; 102 } 145 } 146 } 147 103 148 if ($found) 104 149 { 105 150 $name = $found; 106 151 } 152 107 153 $columns[] = new sfDoctrineAdminColumn($name, $col); 108 154 } … … 111 157 } 112 158 159 /** 160 * Get an sfDoctrineAdminColumn instance for a field/column 161 * 162 * @param string $field 163 * @param string $flag 164 * @return sfDoctrineAdminColumn $column 165 */ 113 166 function getAdminColumnForField($field, $flag = null) 114 167 { … … 117 170 } 118 171 119 172 /** 173 * Get symfony php object helper 174 * 175 * @param string $helperName 176 * @param string $column 177 * @param string $params 178 * @param string $localParams 179 * @return string $helperCode 180 */ 120 181 function getPHPObjectHelper($helperName, $column, $params, $localParams = array()) 121 182 { … … 128 189 } 129 190 130 return sprintf ('object_%s($%s, %s, %s)', $helperName, $this->getSingularName(), var_export($this->getColumnGetter($column), true), $params); 131 } 132 191 return sprintf('object_%s($%s, %s, %s)', $helperName, $this->getSingularName(), var_export($this->getColumnGetter($column), true), $params); 192 } 193 194 /** 195 * Get php code for column getter 196 * 197 * @param string $column 198 * @param string $developed 199 * @param string $prefix 200 * @return string $getterCode 201 */ 133 202 function getColumnGetter($column, $developed = false, $prefix = '') 134 203 { … … 142 211 } 143 212 213 /** 214 * Get php code for column setter 215 * 216 * @param string $column 217 * @param string $value 218 * @param string $singleQuotes 219 * @param string $prefix 220 * @return string $setterCode 221 */ 144 222 function getColumnSetter($column, $value, $singleQuotes = false, $prefix = 'this->') 145 223 { … … 152 230 } 153 231 232 /** 233 * Get related class name for a column 234 * 235 * @param string $column 236 * @return string $className 237 */ 154 238 function getRelatedClassName($column) 155 239 { … … 157 241 } 158 242 243 /** 244 * Get php code for column edit tag 245 * 246 * @param string $column 247 * @param string $params 248 * @return string $columnEditTag 249 */ 159 250 public function getColumnEditTag($column, $params = array()) 160 251 { 161 252 if ($column->getDoctrineType() == 'enum') 162 253 { 163 // FIXME: this is called already in the sfAdminGenerator class!!!164 254 $params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params); 165 255 plugins/sfDoctrinePlugin/trunk/lib/generator/sfDoctrineFormGenerator.class.php
r8743 r8744 93 93 { 94 94 $this->table = Doctrine::getTable($model); 95 $this->modelName = $model; 95 96 96 97 if ($this->isPluginModel($model)) … … 206 207 { 207 208 $names = array(); 208 foreach ($this->table->getColumns() as $column) 209 { 210 if (!$this->isColumnPrimaryKey() && $column->isForeignKey()) 211 { 212 $names[] = array($this->getForeignTable($column)->getOption('name'), $column->getOption('name'), $this->isColumnNotNull($column), false); 213 } 214 } 215 216 foreach ($this->getManyToManyRelations() as $tables) 217 { 218 $names[] = array($tables['relatedTable']->getOption('name'), $tables['middleTable']->getOption('name'), false, true); 209 foreach ($this->table->getRelations() as $relation) 210 { 211 if ($relation->getType() === Doctrine_Relation::ONE) 212 { 213 $foreignDef = $relation->getTable()->getDefinitionOf($relation->getForeignFieldName()); 214 $names[] = array($relation['table']->getOption('name'), $relation->getForeignFieldName(), $this->isColumnNotNull($relation->getForeignFieldName(), $foreignDef), false); 215 } 216 } 217 218 foreach ($this->getManyToManyRelations() as $relation) 219 { 220 $names[] = array($relation['table']->getOption('name'), $relation['refTable']->getOption('name'), false, true); 219 221 } 220 222 … … 229 231 public function getPrimaryKey() 230 232 { 231 foreach ( $this->table->getColumns() as $column)232 { 233 if ($this->isColumnPrimaryKey( ))234 { 235 return $ column;233 foreach (array_keys($this->table->getColumns()) as $name) 234 { 235 if ($this->isColumnPrimaryKey($name)) 236 { 237 return $this->table->getDefinitionOf($name); 236 238 } 237 239 } … … 245 247 * @return string The name of a subclass of sfWidgetForm 246 248 */ 247 public function getWidgetClassForColumn($column) 248 { 249 public function getWidgetClassForColumn($name) 250 { 251 $column = $this->table->getDefinitionOf($name); 249 252 switch ($column['type']) 250 253 { … … 270 273 271 274 272 if ($this->isColumnPrimaryKey($ column))275 if ($this->isColumnPrimaryKey($name)) 273 276 { 274 277 $name = 'InputHidden'; 275 278 } 276 else if ($this->isColumnForeignKey($ column))279 else if ($this->isColumnForeignKey($name)) 277 280 { 278 281 $name = 'DoctrineSelect'; … … 282 285 } 283 286 284 public function isColumnForeignKey($column) 285 { 287 /** 288 * Check if a column is a foreign key 289 * 290 * @param array $column 291 * @return boolean $bool 292 */ 293 public function isColumnForeignKey($name) 294 { 295 foreach ($this->table->getRelations() as $relation) 296 { 297 if ($relation['local'] == $name) 298 { 299 return true; 300 } 301 } 286 302 return false; 287 303 } 288 304 289 public function isColumnPrimaryKey($column) 290 { 305 /** 306 * Get the foreign table object for a column 307 * 308 * @param string $name 309 * @param string $column 310 * @return Doctrine_Table $table 311 */ 312 public function getForeignTable($name) 313 { 314 foreach ($this->table->getRelations() as $relation) 315 { 316 if ($relation['local'] == $name) 317 { 318 return $relation['table']; 319 } 320 } 321 return false; 322 } 323 324 /** 325 * Check if a column is a primary key 326 * 327 * @param string $name 328 * @return boolean $bool 329 */ 330 public function isColumnPrimaryKey($name) 331 { 332 $column = $this->table->getDefinitionOf($name); 291 333 return (isset($column['primary']) && $column['primary']); 292 334 } 293 335 294 public function isColumnNotNull($column) 295 { 336 /** 337 * Check if a column is not null 338 * 339 * @param array $column 340 * @return boolean $bool 341 */ 342 public function isColumnNotNull($name) 343 { 344 $column = $this->table->getDefinitionOf($name); 296 345 return (isset($column['notnull']) && $column['notnull']); 297 346 } … … 304 353 * @return string The options to pass to the widget as a PHP string 305 354 */ 306 public function getWidgetOptionsForColumn($column) 307 { 355 public function getWidgetOptionsForColumn($name) 356 { 357 $column = $this->table->getDefinitionOf($name); 308 358 $options = array(); 309 359 310 if (! isset($column['primary']) && $this->isColumnForeignKey($column))311 { 312 $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => %s', $this->getForeignTable($ column)->getOption('name'), isset($column['notnull']) ? 'false' : 'true');360 if (!$this->isColumnPrimaryKey($name) && $this->isColumnForeignKey($name)) 361 { 362 $options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => %s', $this->getForeignTable($name)->getOption('name'), isset($column['notnull']) ? 'false' : 'true'); 313 363 } 314 364 … … 323 373 * @return string The name of a subclass of sfValidator 324 374 */ 325 public function getValidatorClassForColumn($column) 326 { 375 public function getValidatorClassForColumn($colName) 376 { 377 $column = $this->table->getDefinitionOf($colName); 327 378 switch ($column['type']) 328 379 { … … 356 407 } 357 408 358 if ($this->isColumnPrimaryKey($col umn) || $this->isColumnForeignKey($column))409 if ($this->isColumnPrimaryKey($colName) || $this->isColumnForeignKey($colName)) 359 410 { 360 411 $name = 'DoctrineChoice'; … … 371 422 * @return string The options to pass to the validator as a PHP string 372 423 */ 373 public function getValidatorOptionsForColumn($name, $column) 374 { 424 public function getValidatorOptionsForColumn($name) 425 { 426 $column = $this->table->getDefinitionOf($name); 375 427 $options = array(); 376 428 377 if ($this->isColumnForeignKey($ column))378 { 379 $options[] = sprintf('\'model\' => \'%s\'', $this->getForeignTable($ column)->getOption('name'));380 } 381 else if ($this->isColumnPrimaryKey($ column))382 { 383 $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this-> table->getOption('name'), $name);429 if ($this->isColumnForeignKey($name)) 430 { 431 $options[] = sprintf('\'model\' => \'%s\'', $this->getForeignTable($name)->getOption('name')); 432 } 433 else if ($this->isColumnPrimaryKey($name)) 434 { 435 $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $name); 384 436 } 385 437 else … … 396 448 } 397 449 398 if (!$this->isColumnNotNull($ column) || $this->isColumnPrimaryKey())450 if (!$this->isColumnNotNull($name) || $this->isColumnPrimaryKey($name)) 399 451 { 400 452 $options[] = '\'required\' => false'; … … 420 472 } 421 473 422 foreach ($this->getManyToManyRelations() as $ tables)423 { 424 if (($m = strlen($this->underscore($ tables['refTable']->getOption('name')).'_list')) > $max)474 foreach ($this->getManyToManyRelations() as $relation) 475 { 476 if (($m = strlen($this->underscore($relation['refTable']->getOption('name')).'_list')) > $max) 425 477 { 426 478 $max = $m; … … 438 490 public function getPrimaryKeyColumNames() 439 491 { 440 return $this->table->getIdentifier ();492 return $this->table->getIdentifierColumnNames(); 441 493 } 442 494 … … 460 512 public function isI18n() 461 513 { 462 return method_exists($this->table->getOption('name').'Peer', 'getI18nModel');514 return $this->table->hasRelation('Translation'); 463 515 } 464 516 … … 470 522 public function getI18nModel() 471 523 { 472 return call_user_func(array($this->table->getOption('name').'Peer', 'getI18nModel'));524 return $this->table->getRelation('Translation')->getTable()->create(); 473 525 } 474 526