isI18n() && !isset($this->Translation)) { // lazy load translations $this->getObject()->loadReference('Translation'); } // update defaults for the main object if ($this->isNew()) { $this->setDefaults(sfForm::deepArrayUnion($this->object->toArray(true), $this->getDefaults())); } else { $this->setDefaults(sfForm::deepArrayUnion($this->getDefaults(), $this->object->toArray(true))); } if ($this->isI18n()) { $defaults = $this->getDefaults(); $translations = $defaults['Translation']; unset($defaults['Translation']); $this->setDefaults(sfForm::deepArrayUnion($defaults, $translations)); } } protected static function removeSpecialColumns(array $values) { unset($values['id'], $values['updated_at'], $values['updated_on'], $values['created_at'], $values['created_on']); foreach ($values as $key => $value) { if (is_array($value)){ $values[$key] = self::removeSpecialColumns($value); } } return $values; } /** * Updates the values of the object with the cleaned up values. * * @return BaseObject The current updated object */ public function updateObject() { if (!$this->isValid()) { throw $this->getErrorSchema(); } // remove special columns that are updated automatically $values = self::removeSpecialColumns($this->getValues()); // Move translations to the Translation key so that it will work with Doctrine_Record::fromArray() foreach ($this->cultures as $culture) { $translation = $values[$culture]; $translation['lang'] = $culture; unset($translation['id']); $values['Translation'][$culture] = $translation; unset($values[$culture]); } // changed to deep reloading $this->object->fromArray($values, true); return $this->object; } public $_embeddedObjects = array(); protected function hasEmbeddedObject(Doctrine_Record $object, $register = false) { $keys = array($object->getTable()->getComponentName()); $identifiers = (array) $object->getTable()->getIdentifier(); foreach ($identifiers as $identifier){ $keys[] = (string)$object[$identifier]; } $uniqueKey = implode('-',$keys); // echo "
".print_r($uniqueKey,true).""; // echo "
".print_r($object->toArray(),true).""; $result = isset($this->_embeddedObjects[$uniqueKey]); if (!$result && $register) $this->_embeddedObjects[$uniqueKey] = $object; return $result; } protected function doSave($con = null) { if (is_null($con)) { $con = $this->getConnection(); } parent::doSave($con); foreach ($this->_embeddedObjects as $object){ if ($object->isModified()) { $object->save($con); } } } public function embedRelation($relationName, $formClass = null, $options = array()) { $table = $this->object->getTable(); if (!$table->hasRelation($relationName)) { throw new RuntimeException(sprintf('Relation "%s" does not exist on table "%s"',$relationName,$table->getTableName())); } $relation = $table->getRelation($relationName); $this->object->refreshRelated($relationName); $formClass = is_null($formClass)?$relation->getClass()."Form":$formClass; if (!class_exists($formClass)) { throw new RuntimeException(sprintf('Form of class "%s" does not exist',$formClass)); } $options = sfForm::deepArrayUnion($this->options, $options); //register original calling form, used for recursion protection if (!isset($options['embedRelation_root_form'])) { $options['embedRelation_root_form'] = $this; } $rootForm = $options['embedRelation_root_form']; if ($relation->isOneToOne()) { if ($this->object[$relationName]->exists()) { if (!$rootForm->hasEmbeddedObject($this->object[$relationName], true)) { $form = new $formClass($this->object[$relationName],$options); unset($form[$relation->getForeign()]); $this->embedForm($relationName, $form); if (isset($this[$relation->getLocal()])) { //replace the current form element with the new embedded form $this->widgetSchema->moveField($relationName, sfWidgetFormSchema::AFTER, $relation->getLocal()); $this->getWidget($relation->getLocal())->setAttribute('disabled', true); // unset($this[$relation->getLocal()]); } } else { $this->widgetSchema->setHelp($relation->getLocal(), "Recursion detected: You can edit this object in detail somewhere else on this form"); } } } elseif (count($this->object[$relationName]) > 0) { $subForm = new sfForm(); foreach ($this->object[$relationName] as $index => $childObject) { if (!$rootForm->hasEmbeddedObject($childObject, true)) { $form = new $formClass($childObject,$options); unset($form[$relation->getForeign()]); $subForm->embedForm((string)$index, $form); } else { $this->setWidget((string)$index, new sfWidgetFormInput(array('type'=>'hidden'),array())); $this->setValidator((string)$index, new sfValidatorString(array('required'=>false))); $this->widgetSchema->setHelp((string)$index, "Recursion detected: You can edit this object somewhere else on this form"); } } $this->embedForm($relationName, $subForm); } } public function embedRelations($prefix = "", $options = array()) { $options = sfForm::deepArrayUnion($this->options, $options); foreach ($this->object->getTable()->getRelations() as $relation) { $formClass = $prefix.$relation->getClass()."Form"; $this->embedRelation($relation->getAlias(),$formClass,$options); } } }