/lib/plugins/sfPropelPlugin/data/generator/sfPropelForm/default/template/sfPropelFormGeneratedTemplate.php

https://github.com/bheneka/gitta · PHP · 139 lines · 108 code · 21 blank · 10 comment · 9 complexity · 794ae67c70ac325803d475a7bdf47c42 MD5 · raw file

  1. [?php
  2. /**
  3. * <?php echo $this->table->getClassname() ?> form base class.
  4. *
  5. * @method <?php echo $this->table->getClassname() ?> getObject() Returns the current form's model object
  6. *
  7. * @package ##PROJECT_NAME##
  8. * @subpackage form
  9. * @author ##AUTHOR_NAME##
  10. */
  11. abstract class Base<?php echo $this->table->getClassname() ?>Form extends BaseFormPropel
  12. {
  13. public function setup()
  14. {
  15. $this->setWidgets(array(
  16. <?php foreach ($this->table->getColumns() as $column): ?>
  17. '<?php echo $this->translateColumnName($column) ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($column->getName())) ?> => new <?php echo $this->getWidgetClassForColumn($column) ?>(<?php echo $this->getWidgetOptionsForColumn($column) ?>),
  18. <?php endforeach; ?>
  19. <?php foreach ($this->getManyToManyTables() as $tables): ?>
  20. '<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($this->underscore($tables['middleTable']->getClassname()).'_list')) ?> => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => '<?php echo $tables['relatedTable']->getClassname() ?>')),
  21. <?php endforeach; ?>
  22. ));
  23. $this->setValidators(array(
  24. <?php foreach ($this->table->getColumns() as $column): ?>
  25. '<?php echo $this->translateColumnName($column) ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($column->getName())) ?> => new <?php echo $this->getValidatorClassForColumn($column) ?>(<?php echo $this->getValidatorOptionsForColumn($column) ?>),
  26. <?php endforeach; ?>
  27. <?php foreach ($this->getManyToManyTables() as $tables): ?>
  28. '<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($this->underscore($tables['middleTable']->getClassname()).'_list')) ?> => new sfValidatorPropelChoice(array('multiple' => true, 'model' => '<?php echo $tables['relatedTable']->getClassname() ?>', 'required' => false)),
  29. <?php endforeach; ?>
  30. ));
  31. <?php if ($uniqueColumns = $this->getUniqueColumnNames()): ?>
  32. $this->validatorSchema->setPostValidator(
  33. <?php if (count($uniqueColumns) > 1): ?>
  34. new sfValidatorAnd(array(
  35. <?php foreach ($uniqueColumns as $uniqueColumn): ?>
  36. new sfValidatorPropelUnique(array('model' => '<?php echo $this->table->getClassname() ?>', 'column' => array('<?php echo implode("', '", $uniqueColumn) ?>'))),
  37. <?php endforeach; ?>
  38. ))
  39. <?php else: ?>
  40. new sfValidatorPropelUnique(array('model' => '<?php echo $this->table->getClassname() ?>', 'column' => array('<?php echo implode("', '", $uniqueColumns[0]) ?>')))
  41. <?php endif; ?>
  42. );
  43. <?php endif; ?>
  44. $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->table->getClassname()) ?>[%s]');
  45. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  46. parent::setup();
  47. }
  48. public function getModelName()
  49. {
  50. return '<?php echo $this->table->getClassname() ?>';
  51. }
  52. <?php if ($this->isI18n()): ?>
  53. public function getI18nModelName()
  54. {
  55. return '<?php echo $this->getI18nModel() ?>';
  56. }
  57. public function getI18nFormClass()
  58. {
  59. return '<?php echo $this->getI18nModel() ?>Form';
  60. }
  61. <?php endif; ?>
  62. <?php if ($this->getManyToManyTables()): ?>
  63. public function updateDefaultsFromObject()
  64. {
  65. parent::updateDefaultsFromObject();
  66. <?php foreach ($this->getManyToManyTables() as $tables): ?>
  67. if (isset($this->widgetSchema['<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list']))
  68. {
  69. $values = array();
  70. foreach ($this->object->get<?php echo $tables['middleTable']->getPhpName() ?>s() as $obj)
  71. {
  72. $values[] = $obj->get<?php echo $tables['relatedColumn']->getPhpName() ?>();
  73. }
  74. $this->setDefault('<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list', $values);
  75. }
  76. <?php endforeach; ?>
  77. }
  78. protected function doSave($con = null)
  79. {
  80. parent::doSave($con);
  81. <?php foreach ($this->getManyToManyTables() as $tables): ?>
  82. $this->save<?php echo $tables['middleTable']->getPhpName() ?>List($con);
  83. <?php endforeach; ?>
  84. }
  85. <?php foreach ($this->getManyToManyTables() as $tables): ?>
  86. public function save<?php echo $tables['middleTable']->getPhpName() ?>List($con = null)
  87. {
  88. if (!$this->isValid())
  89. {
  90. throw $this->getErrorSchema();
  91. }
  92. if (!isset($this->widgetSchema['<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list']))
  93. {
  94. // somebody has unset this widget
  95. return;
  96. }
  97. if (null === $con)
  98. {
  99. $con = $this->getConnection();
  100. }
  101. $c = new Criteria();
  102. $c->add(<?php echo constant($tables['middleTable']->getClassname().'::PEER') ?>::<?php echo strtoupper($tables['column']->getName()) ?>, $this->object->getPrimaryKey());
  103. <?php echo constant($tables['middleTable']->getClassname().'::PEER') ?>::doDelete($c, $con);
  104. $values = $this->getValue('<?php echo $this->underscore($tables['middleTable']->getClassname()) ?>_list');
  105. if (is_array($values))
  106. {
  107. foreach ($values as $value)
  108. {
  109. $obj = new <?php echo $tables['middleTable']->getClassname() ?>();
  110. $obj->set<?php echo $tables['column']->getPhpName() ?>($this->object->getPrimaryKey());
  111. $obj->set<?php echo $tables['relatedColumn']->getPhpName() ?>($value);
  112. $obj->save();
  113. }
  114. }
  115. }
  116. <?php endforeach; ?>
  117. <?php endif; ?>
  118. }