Development

Changeset 8744

You must first sign up to be able to contribute.

Changeset 8744

Show
Ignore:
Timestamp:
05/03/08 07:53:43 (7 months ago)
Author:
Jonathan.Wage
Message:

Fixes to form and admin generator.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfDoctrinePlugin/trunk/data/generator/sfDoctrineForm/default/template/sfDoctrineFormGeneratedTemplate.php

    r8726 r8744  
    22 
    33/** 
    4  * <?php echo $this->table->getOption('name') ?> form base class. 
     4 * <?php echo $this->modelName ?> form base class. 
    55 * 
    66 * @package    form 
    7  * @subpackage <?php echo $this->underscore($this->table->getOption('name')) ?> 
     7 * @subpackage <?php echo $this->underscore($this->modelName) ?> 
    88 
    99 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $ 
    1010 */ 
    11 class Base<?php echo $this->table->getOption('name') ?>Form extends BaseFormDoctrine 
     11class Base<?php echo $this->modelName ?>Form extends BaseFormDoctrine 
    1212{ 
    1313  public function setup() 
     
    1515    $this->setWidgets(array( 
    1616<?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) ?>), 
    1818<?php endforeach; ?> 
    1919<?php foreach ($this->getManyToManyRelations() as $relation): ?> 
     
    2424    $this->setValidators(array( 
    2525<?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) ?>), 
    2727<?php endforeach; ?> 
    2828<?php foreach ($this->getManyToManyRelations() as $relation): ?> 
     
    3131    )); 
    3232 
    33     $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->table->getOption('name')) ?>[%s]'); 
     33    $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->modelName) ?>[%s]'); 
    3434 
    3535    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); 
     
    4040  public function getModelName() 
    4141  { 
    42     return '<?php echo $this->table->getOption('name') ?>'; 
     42    return '<?php echo $this->modelName ?>'; 
    4343  } 
    4444<?php if ($this->isI18n()): ?> 
  • plugins/sfDoctrinePlugin/trunk/lib/generator/sfDoctrineAdminGenerator.class.php

    r8743 r8744  
    1717class sfDoctrineAdminGenerator extends sfAdminGenerator 
    1818{ 
     19  /** 
     20   * Doctrine_Table instance for this admin generator 
     21   * 
     22   * @var Doctrine_Table $table 
     23   */ 
    1924  protected $table; 
    2025 
     26  /** 
     27   * Initialize a doctrine admin generator instance 
     28   * 
     29   * @param sfGeneratorManager $generatorManager 
     30   * @return void 
     31   */ 
    2132  public function initialize(sfGeneratorManager $generatorManager) 
    2233  { 
     
    2637  } 
    2738 
     39  /** 
     40   * Load all Doctrine_Table class to generate instance 
     41   * 
     42   * @return void 
     43   */ 
    2844  protected function loadMapBuilderClasses() 
    2945  { 
     
    3147  } 
    3248 
     49  /** 
     50   * Get Doctrine_Table instance for admin generator 
     51   * 
     52   * @return Doctrine_Table $table 
     53   */ 
    3354  protected function getTable() 
    3455  { 
     
    3657  } 
    3758 
     59  /** 
     60   * Load primary key columns as array of sfDoctrineAdminColumn instances 
     61   * 
     62   * @return void 
     63   * @throws sfException 
     64   */ 
    3865  protected function loadPrimaryKeys() 
    3966  { 
     
    5582  } 
    5683 
     84  /** 
     85   * Get columns for admin generator instance 
     86   * 
     87   * @param string $paramName  
     88   * @param string $category  
     89   * @return array $columns 
     90   */ 
    5791  public function getColumns($paramName, $category = 'NONE') 
    5892  { 
     
    75109          $column->setColumnName($columnName); 
    76110 
    77           if (isset($cols[$columnName])) // if it is not a many2many 
     111          // if it is not a many2many 
     112          if (isset($cols[$columnName])) 
     113          { 
    78114            $column->setColumnInfo($cols[$columnName]); 
     115          } 
    79116 
    80117          $columns[$index] = $column; 
     
    86123  } 
    87124 
     125  /** 
     126   * Get array of all columns as sfDoctrineAdminColumn instances 
     127   * 
     128   * @return array $columns 
     129   */ 
    88130  function getAllColumns() 
    89131  { 
     
    95137      // we set out to replace the foreign key to their corresponding aliases 
    96138      $found = null; 
    97       foreach ($rels as $alias=>$rel) 
     139      foreach ($rels as $alias => $rel) 
    98140      { 
    99141        $relType = $rel->getType(); 
    100142        if ($rel->getLocal() == $name && $relType != Doctrine_Relation::MANY_AGGREGATE && $relType != Doctrine_Relation::MANY_COMPOSITE) 
     143        { 
    101144          $found = $alias; 
    102       } 
     145        } 
     146      } 
     147 
    103148      if ($found) 
    104149      { 
    105150        $name = $found; 
    106151      } 
     152 
    107153      $columns[] = new sfDoctrineAdminColumn($name, $col); 
    108154    } 
     
    111157  } 
    112158 
     159  /** 
     160   * Get an sfDoctrineAdminColumn instance for a field/column 
     161   * 
     162   * @param string $field  
     163   * @param string $flag  
     164   * @return sfDoctrineAdminColumn $column 
     165   */ 
    113166  function getAdminColumnForField($field, $flag = null) 
    114167  { 
     
    117170  } 
    118171 
    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   */ 
    120181  function getPHPObjectHelper($helperName, $column, $params, $localParams = array()) 
    121182  { 
     
    128189    } 
    129190 
    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   */ 
    133202  function getColumnGetter($column, $developed = false, $prefix = '') 
    134203  { 
     
    142211  } 
    143212 
     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   */ 
    144222  function getColumnSetter($column, $value, $singleQuotes = false, $prefix = 'this->') 
    145223  { 
     
    152230  } 
    153231 
     232  /** 
     233   * Get related class name for a column 
     234   * 
     235   * @param string $column  
     236   * @return string $className 
     237   */ 
    154238  function getRelatedClassName($column) 
    155239  { 
     
    157241  } 
    158242 
     243  /** 
     244   * Get php code for column edit tag 
     245   * 
     246   * @param string $column  
     247   * @param string $params  
     248   * @return string $columnEditTag 
     249   */ 
    159250  public function getColumnEditTag($column, $params = array()) 
    160251  { 
    161252    if ($column->getDoctrineType() == 'enum') 
    162253    { 
    163       // FIXME: this is called already in the sfAdminGenerator class!!! 
    164254      $params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params); 
    165255 
  • plugins/sfDoctrinePlugin/trunk/lib/generator/sfDoctrineFormGenerator.class.php

    r8743 r8744  
    9393    { 
    9494      $this->table = Doctrine::getTable($model); 
     95      $this->modelName = $model; 
    9596 
    9697      if ($this->isPluginModel($model)) 
     
    206207  { 
    207208    $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); 
    219221    } 
    220222 
     
    229231  public function getPrimaryKey() 
    230232  { 
    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)
    236238      } 
    237239    } 
     
    245247   * @return string    The name of a subclass of sfWidgetForm 
    246248   */ 
    247   public function getWidgetClassForColumn($column) 
    248   { 
     249  public function getWidgetClassForColumn($name) 
     250  { 
     251    $column = $this->table->getDefinitionOf($name); 
    249252    switch ($column['type']) 
    250253    { 
     
    270273 
    271274 
    272     if ($this->isColumnPrimaryKey($column)) 
     275    if ($this->isColumnPrimaryKey($name)) 
    273276    { 
    274277      $name = 'InputHidden'; 
    275278    } 
    276     else if ($this->isColumnForeignKey($column)) 
     279    else if ($this->isColumnForeignKey($name)) 
    277280    { 
    278281      $name = 'DoctrineSelect'; 
     
    282285  } 
    283286 
    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    } 
    286302    return false; 
    287303  } 
    288304 
    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); 
    291333    return (isset($column['primary']) && $column['primary']); 
    292334  } 
    293335 
    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); 
    296345    return (isset($column['notnull']) && $column['notnull']); 
    297346  } 
     
    304353   * @return string    The options to pass to the widget as a PHP string 
    305354   */ 
    306   public function getWidgetOptionsForColumn($column) 
    307   { 
     355  public function getWidgetOptionsForColumn($name) 
     356  { 
     357    $column = $this->table->getDefinitionOf($name); 
    308358    $options = array(); 
    309359 
    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'); 
    313363    } 
    314364 
     
    323373   * @return string    The name of a subclass of sfValidator 
    324374   */ 
    325   public function getValidatorClassForColumn($column) 
    326   { 
     375  public function getValidatorClassForColumn($colName) 
     376  { 
     377    $column = $this->table->getDefinitionOf($colName); 
    327378    switch ($column['type']) 
    328379    { 
     
    356407    } 
    357408 
    358     if ($this->isColumnPrimaryKey($column) || $this->isColumnForeignKey($column)) 
     409    if ($this->isColumnPrimaryKey($colName) || $this->isColumnForeignKey($colName)) 
    359410    { 
    360411      $name = 'DoctrineChoice'; 
     
    371422   * @return string    The options to pass to the validator as a PHP string 
    372423   */ 
    373   public function getValidatorOptionsForColumn($name, $column) 
    374   { 
     424  public function getValidatorOptionsForColumn($name) 
     425  { 
     426    $column = $this->table->getDefinitionOf($name); 
    375427    $options = array(); 
    376428 
    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); 
    384436    } 
    385437    else 
     
    396448    } 
    397449 
    398     if (!$this->isColumnNotNull($column) || $this->isColumnPrimaryKey()) 
     450    if (!$this->isColumnNotNull($name) || $this->isColumnPrimaryKey($name)) 
    399451    { 
    400452      $options[] = '\'required\' => false'; 
     
    420472    } 
    421473 
    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) 
    425477      { 
    426478        $max = $m; 
     
    438490  public function getPrimaryKeyColumNames() 
    439491  { 
    440     return $this->table->getIdentifier(); 
     492    return $this->table->getIdentifierColumnNames(); 
    441493  } 
    442494 
     
    460512  public function isI18n() 
    461513  { 
    462     return method_exists($this->table->getOption('name').'Peer', 'getI18nModel'); 
     514    return $this->table->hasRelation('Translation'); 
    463515  } 
    464516 
     
    470522  public function getI18nModel() 
    471523  { 
    472     return call_user_func(array($this->table->getOption('name').'Peer', 'getI18nModel')); 
     524    return $this->table->getRelation('Translation')->getTable()->create(); 
    473525  } 
    474526