Skip to content

Instantly share code, notes, and snippets.

@johnkary
Created June 13, 2011 15:56
Show Gist options
  • Save johnkary/1023053 to your computer and use it in GitHub Desktop.
Save johnkary/1023053 to your computer and use it in GitHub Desktop.
Assign sfDoctrineActAsKeyValueStorePlugin object values with forms
<?php
/**
* Allows key-value store values from objects with the ActAsKeyValueStore Doctrine
* behavior to be populated into form value defaults and also assigned/persisted
* to with the object using the form.
*
* @author John Kary <johnkary@ku.edu>
*/
abstract class KeyValueStoreForm extends sfFormDoctrine
{
/**
* Gets object property values and KeyValueStore values.
*
* @return array
*/
protected function getObjectValues()
{
$values = $this->getObject()->toArray(false) + $this->getObjectKeyValueStoreValues();
return $values;
}
/**
* Gets object values for fields representing values presisted using
* Doctrine behavior KeyValueStore.
*
* Customize for each form using an object with KeyValueStore behavior.
*
* @return array
*/
protected function getObjectKeyValueStoreValues()
{
return array();
}
/**
* Updates the default values of the form with the current values of the
* current object.
*
* Added support for defaults that come from Doctrine behavior KeyValueStore.
*/
protected function updateDefaultsFromObject()
{
$defaults = $this->getDefaults();
// update defaults for the main object
if ($this->isNew()) {
$defaults = $defaults + $this->getObjectValues();
} else {
$defaults = $this->getObjectValues() + $defaults;
}
foreach ($this->embeddedForms as $name => $form) {
if ($form instanceof sfFormDoctrine) {
$form->updateDefaultsFromObject();
$defaults[$name] = $form->getDefaults();
}
}
$this->setDefaults($defaults);
}
/**
* Updates the values of the object with the cleaned up values.
*
* Overrides default Doctrine implementation to add storage of KeyValueStore
*
* @param array $values An array of values
* @return mixed The current updated object
*/
public function updateObject($values = null)
{
if (null === $values) {
$values = $this->values;
}
$values = $this->processValues($values);
$this->doUpdateObject($values);
//Store form values that are not object properties as key-value store data
$this->updateObjectKeyValueStoreValues($values);
// embedded forms
$this->updateObjectEmbeddedForms($values);
return $this->getObject();
}
/**
* Sets object values for fields representing values persisted using
* Doctrine behavior KeyValueStore.
*
* Customize for each form using an object with KeyValueStore behavior.
*
* @param array $values
*/
protected function updateObjectKeyValueStoreValues(array $values)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment