1. sfFormObject.class.php
  2. /** * Base class for forms that deal with a single object. * * @package symfony * @subpackage form * @author Kris Wallsmith * @version SVN: $Id: sfFormObject.class.php 22917 2009-10-10 13:44:53Z Kris.Wallsmith $ */
  3. abstract class sfFormObject extends BaseForm
  4. {
  5. protected
  6. $isNew = true,
  7. $object = null;
  8. /**
  9. * Returns the current model name.
  10. *
  11. * @return string
  12. */
  13. abstract public function getModelName();
  14. /**
  15. * Returns the default connection for the current model.
  16. *
  17. * @return mixed A database connection
  18. */
  19. abstract public function getConnection();
  20. /**
  21. * Updates the values of the object with the cleaned up values.
  22. *
  23. * If you want to add some logic before updating or update other associated
  24. * objects, this is the method to override.
  25. *
  26. * @param array $values An array of values
  27. */
  28. abstract protected function doUpdateObject($values);
  29. /**
  30. * Processes cleaned up values.
  31. *
  32. * @param array $values An array of values
  33. *
  34. * @return array An array of cleaned up values
  35. */
  36. abstract public function processValues($values);
  37. /**
  38. * Returns true if the current form embeds a new object.
  39. *
  40. * @return Boolean true if the current form embeds a new object, false otherwise
  41. */
  42. public function isNew()
  43. {
  44. return $this->isNew;
  45. }
  46. /**
  47. * Returns the current object for this form.
  48. *
  49. * @return mixed The current object
  50. */
  51. public function getObject()
  52. {
  53. return $this->object;
  54. }
  55. /**
  56. * Binds the current form and saves the object to the database in one step.
  57. *
  58. * @param array An array of tainted values to use to bind the form
  59. * @param array An array of uploaded files (in the $_FILES or $_GET format)
  60. * @param mixed An optional connection object
  61. *
  62. * @return Boolean true if the form is valid, false otherwise
  63. */
  64. public function bindAndSave($taintedValues, $taintedFiles = null, $con = null)
  65. {
  66. $this->bind($taintedValues, $taintedFiles);
  67. if ($this->isValid())
  68. {
  69. $this->save($con);
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Saves the current object to the database.
  76. *
  77. * The object saving is done in a transaction and handled by the doSave() method.
  78. *
  79. * @param mixed $con An optional connection object
  80. *
  81. * @return mixed The current saved object
  82. *
  83. * @see doSave()
  84. *
  85. * @throws sfValidatorError If the form is not valid
  86. */
  87. public function save($con = null)
  88. {
  89. if (!$this->isValid())
  90. {
  91. throw $this->getErrorSchema();
  92. }
  93. if (null === $con)
  94. {
  95. $con = $this->getConnection();
  96. }
  97. try
  98. {
  99. $con->beginTransaction();
  100. $this->doSave($con);
  101. $con->commit();
  102. }
  103. catch (Exception $e)
  104. {
  105. $con->rollBack();
  106. throw $e;
  107. }
  108. return $this->getObject();
  109. }
  110. /**
  111. * Updates and saves the current object.
  112. *
  113. * If you want to add some logic before saving or save other associated
  114. * objects, this is the method to override.
  115. *
  116. * @param mixed $con An optional connection object
  117. */
  118. protected function doSave($con = null)
  119. {
  120. if (null === $con)
  121. {
  122. $con = $this->getConnection();
  123. }
  124. $this->updateObject();
  125. $this->getObject()->save($con);
  126. // embedded forms
  127. $this->saveEmbeddedForms($con);
  128. }
  129. /**
  130. * Updates the values of the object with the cleaned up values.
  131. *
  132. * @param array $values An array of values
  133. *
  134. * @return mixed The current updated object
  135. */
  136. public function updateObject($values = null)
  137. {
  138. if (null === $values)
  139. {
  140. $values = $this->values;
  141. }
  142. $values = $this->processValues($values);
  143. $this->doUpdateObject($values);
  144. // embedded forms
  145. $this->updateObjectEmbeddedForms($values);
  146. return $this->getObject();
  147. }
  148. /**
  149. * Updates the values of the objects in embedded forms.
  150. *
  151. * @param array $values An array of values
  152. * @param array $forms An array of forms
  153. */
  154. public function updateObjectEmbeddedForms($values, $forms = null)
  155. {
  156. if (null === $forms)
  157. {
  158. $forms = $this->embeddedForms;
  159. }
  160. foreach ($forms as $name => $form)
  161. {
  162. if (!isset($values[$name]) || !is_array($values[$name]))
  163. {
  164. continue;
  165. }
  166. if ($form instanceof sfFormObject)
  167. {
  168. $form->updateObject($values[$name]);
  169. }
  170. else
  171. {
  172. $this->updateObjectEmbeddedForms($values[$name], $form->getEmbeddedForms());
  173. }
  174. }
  175. }
  176. /**
  177. * Saves embedded form objects.
  178. *
  179. * @param mixed $con An optional connection object
  180. * @param array $forms An array of forms
  181. */
  182. public function saveEmbeddedForms($con = null, $forms = null)
  183. {
  184. if (null === $con)
  185. {
  186. $con = $this->getConnection();
  187. }
  188. if (null === $forms)
  189. {
  190. $forms = $this->embeddedForms;
  191. }
  192. foreach ($forms as $form)
  193. {
  194. if ($form instanceof sfFormObject)
  195. {
  196. $form->saveEmbeddedForms($con);
  197. $form->getObject()->save($con);
  198. }
  199. else
  200. {
  201. $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
  202. }
  203. }
  204. }
  205. /**
  206. * Renders a form tag suitable for the related object.
  207. *
  208. * The method is automatically guessed based on the Doctrine object:
  209. *
  210. * * if the object is new, the method is POST
  211. * * if the object already exists, the method is PUT
  212. *
  213. * @param string $url The URL for the action
  214. * @param array $attributes An array of HTML attributes
  215. *
  216. * @return string An HTML representation of the opening form tag
  217. *
  218. * @see sfForm
  219. */
  220. public function renderFormTag($url, array $attributes = array())
  221. {
  222. if (!isset($attributes['method']))
  223. {
  224. $attributes['method'] = $this->isNew() ? 'post' : 'put';
  225. }
  226. return parent::renderFormTag($url, $attributes);
  227. }
  228. protected function camelize($text)
  229. {
  230. return preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'), array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text);
  231. }
  232. }

Debug toolbar