/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineForm/default/template/sfDoctrineFormGeneratedTemplate.php

https://github.com/bheneka/gitta · PHP · 127 lines · 96 code · 20 blank · 11 comment · 10 complexity · 03e2ec73669bc9e339ec22d963cf1266 MD5 · raw file

  1. [?php
  2. /**
  3. * <?php echo $this->modelName ?> form base class.
  4. *
  5. * @method <?php echo $this->modelName ?> getObject() Returns the current form's model object
  6. *
  7. * @package ##PROJECT_NAME##
  8. * @subpackage form
  9. * @author ##AUTHOR_NAME##
  10. * @version SVN: $Id$
  11. */
  12. abstract class Base<?php echo $this->modelName ?>Form extends <?php echo $this->getFormClassToExtend().PHP_EOL ?>
  13. {
  14. public function setup()
  15. {
  16. $this->setWidgets(array(
  17. <?php foreach ($this->getColumns() as $column): ?>
  18. '<?php echo $column->getFieldName() ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($column->getFieldName())) ?> => new <?php echo $this->getWidgetClassForColumn($column) ?>(<?php echo $this->getWidgetOptionsForColumn($column) ?>),
  19. <?php endforeach; ?>
  20. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  21. '<?php echo $this->underscore($relation['alias']) ?>_list'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($this->underscore($relation['alias']).'_list')) ?> => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => '<?php echo $relation['table']->getOption('name') ?>')),
  22. <?php endforeach; ?>
  23. ));
  24. $this->setValidators(array(
  25. <?php foreach ($this->getColumns() as $column): ?>
  26. '<?php echo $column->getFieldName() ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($column->getFieldName())) ?> => new <?php echo $this->getValidatorClassForColumn($column) ?>(<?php echo $this->getValidatorOptionsForColumn($column) ?>),
  27. <?php endforeach; ?>
  28. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  29. '<?php echo $this->underscore($relation['alias']) ?>_list'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($this->underscore($relation['alias']).'_list')) ?> => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => '<?php echo $relation['table']->getOption('name') ?>', 'required' => false)),
  30. <?php endforeach; ?>
  31. ));
  32. <?php if ($uniqueColumns = $this->getUniqueColumnNames()): ?>
  33. $this->validatorSchema->setPostValidator(
  34. <?php if (count($uniqueColumns) > 1): ?>
  35. new sfValidatorAnd(array(
  36. <?php foreach ($uniqueColumns as $uniqueColumn): ?>
  37. new sfValidatorDoctrineUnique(array('model' => '<?php echo $this->table->getOption('name') ?>', 'column' => array('<?php echo implode("', '", $uniqueColumn) ?>'))),
  38. <?php endforeach; ?>
  39. ))
  40. <?php else: ?>
  41. new sfValidatorDoctrineUnique(array('model' => '<?php echo $this->table->getOption('name') ?>', 'column' => array('<?php echo implode("', '", $uniqueColumns[0]) ?>')))
  42. <?php endif; ?>
  43. );
  44. <?php endif; ?>
  45. $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->modelName) ?>[%s]');
  46. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  47. $this->setupInheritance();
  48. parent::setup();
  49. }
  50. public function getModelName()
  51. {
  52. return '<?php echo $this->modelName ?>';
  53. }
  54. <?php if ($this->getManyToManyRelations()): ?>
  55. public function updateDefaultsFromObject()
  56. {
  57. parent::updateDefaultsFromObject();
  58. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  59. if (isset($this->widgetSchema['<?php echo $this->underscore($relation['alias']) ?>_list']))
  60. {
  61. $this->setDefault('<?php echo $this->underscore($relation['alias']) ?>_list', $this->object-><?php echo $relation['alias']; ?>->getPrimaryKeys());
  62. }
  63. <?php endforeach; ?>
  64. }
  65. protected function doSave($con = null)
  66. {
  67. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  68. $this->save<?php echo $relation['alias'] ?>List($con);
  69. <?php endforeach; ?>
  70. parent::doSave($con);
  71. }
  72. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  73. public function save<?php echo $relation['alias'] ?>List($con = null)
  74. {
  75. if (!$this->isValid())
  76. {
  77. throw $this->getErrorSchema();
  78. }
  79. if (!isset($this->widgetSchema['<?php echo $this->underscore($relation['alias']) ?>_list']))
  80. {
  81. // somebody has unset this widget
  82. return;
  83. }
  84. if (null === $con)
  85. {
  86. $con = $this->getConnection();
  87. }
  88. $existing = $this->object-><?php echo $relation['alias']; ?>->getPrimaryKeys();
  89. $values = $this->getValue('<?php echo $this->underscore($relation['alias']) ?>_list');
  90. if (!is_array($values))
  91. {
  92. $values = array();
  93. }
  94. $unlink = array_diff($existing, $values);
  95. if (count($unlink))
  96. {
  97. $this->object->unlink('<?php echo $relation['alias'] ?>', array_values($unlink));
  98. }
  99. $link = array_diff($values, $existing);
  100. if (count($link))
  101. {
  102. $this->object->link('<?php echo $relation['alias'] ?>', array_values($link));
  103. }
  104. }
  105. <?php endforeach; ?>
  106. <?php endif; ?>
  107. }