/**
  * Adds the function body for the save method
  * @param      string &$script The script will be modified in this method.
  * @see        addSave()
  **/
 protected function addSaveBody(&$script)
 {
     $add = false;
     $columns = $this->getTable()->getColumns();
     // created_at, updated_at columns
     $added_now = false;
     $set_now = "\n\t\t\$now = new DateTime('now');";
     foreach ($columns as $column) {
         if ($column->getPhpName() == 'CreatedAt' && $column->getType() == 'TIMESTAMP') {
             $add = true;
             if (!$added_now) {
                 $script .= $set_now;
                 $added_now = true;
             }
             $script .= "\n\t\tif(!\$this->getCreatedAt()) \$this->setCreatedAt(clone \$now);";
         } elseif ($column->getPhpName() == 'UpdatedAt' && $column->getType() == 'TIMESTAMP') {
             $add = true;
             if (!$added_now) {
                 $script .= $set_now;
                 $added_now = true;
             }
             $script .= "\n\t\t\$this->setUpdatedAt(clone \$now);";
         }
     }
     // pass it on
     if ($add) {
         $script .= "\n";
     }
     parent::addSaveBody($script);
 }
Example #2
0
 protected function addSaveBody(&$script)
 {
     $table = $this->getTable();
     if (!$table->containsColumn(self::KALTURA_COLUMN_CUSTOM_DATA)) {
         return parent::addSaveBody($script);
     }
     $reloadOnUpdate = $table->isReloadOnUpdate();
     $reloadOnInsert = $table->isReloadOnInsert();
     $customDataColumn = $table->getColumn(self::KALTURA_COLUMN_CUSTOM_DATA);
     $script .= "\n\t\tif (\$this->isDeleted()) {\n\t\t\tthrow new PropelException(\"You cannot save an object that has been deleted.\");\n\t\t}\n\n\t\tif (\$con === null) {\n\t\t\t\$con = Propel::getConnection(" . $this->getPeerClassname() . "::DATABASE_NAME, Propel::CONNECTION_WRITE);\n\t\t}\n\t\t\n\t\t\$con->beginTransaction();\n\t\t\$isInsert = \$this->isNew();\n\t\ttry {";
     if ($this->getGeneratorConfig()->getBuildProperty('addHooks')) {
         // save with runtime hools
         $script .= "\n\t\t\t\$ret = \$this->preSave(\$con);";
         $this->applyBehaviorModifier('preSave', $script, "\t\t\t");
         $script .= "\n\t\t\tif (\$isInsert) {\n\t\t\t\t\$ret = \$ret && \$this->preInsert(\$con);";
         $this->applyBehaviorModifier('preInsert', $script, "\t\t\t\t");
         $script .= "\n\t\t\t} else {\n\t\t\t\t\$ret = \$ret && \$this->preUpdate(\$con);";
         $this->applyBehaviorModifier('preUpdate', $script, "\t\t\t\t");
         $script .= "\n\t\t\t}\n\t\t\t\n\t\t\tif (!\$ret || !\$this->isModified()) {\n\t\t\t\t\$con->commit();\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\t\n\t\t\tfor (\$retries = 1; \$retries < KalturaPDO::SAVE_MAX_RETRIES; \$retries++)\n\t\t\t{\n               \$affectedRows = \$this->doSave(\$con);\n                if (\$affectedRows || !\$this->isColumnModified(" . $this->getPeerClassname() . "::CUSTOM_DATA)) //ask if custom_data wasn't modified to avoid retry with atomic column \n                \tbreak;\n\n                KalturaLog::debug(\"was unable to save! retrying for the \$retries time\");\n                \$criteria = \$this->buildPkeyCriteria();\n\t\t\t\t\$criteria->addSelectColumn(" . $this->getPeerClassname() . "::CUSTOM_DATA);\n                \$stmt = BasePeer::doSelect(\$criteria, \$con);\n                \$cutsomDataArr = \$stmt->fetchAll(PDO::FETCH_COLUMN);\n                \$newCustomData = \$cutsomDataArr[0];\n\n                \$this->custom_data_md5 = is_null(\$newCustomData) ? null : md5(\$newCustomData);\n\n                \$valuesToChangeTo = \$this->m_custom_data->toArray();\n\t\t\t\t\$this->m_custom_data = myCustomData::fromString(\$newCustomData); \n\n\t\t\t\t//set custom data column values we wanted to change to\n\t\t\t\t\$validUpdate = true;\n\t\t\t\t\$atomicCustomDataFields = " . $this->getPeerClassname() . "::getAtomicCustomDataFields();\n\t\t\t \tforeach (\$this->oldCustomDataValues as \$namespace => \$namespaceValues){\n                \tforeach(\$namespaceValues as \$name => \$oldValue)\n\t\t\t\t\t{\n\t\t\t\t\t\t\$atomicField = false;\n\t\t\t\t\t\tif(\$namespace) {\n\t\t\t\t\t\t\t\$atomicField = array_key_exists(\$namespace, \$atomicCustomDataFields) && in_array(\$name, \$atomicCustomDataFields[\$namespace]);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\$atomicField = in_array(\$name, \$atomicCustomDataFields);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(\$atomicField) {\n\t\t\t\t\t\t\t\$dbValue = \$this->m_custom_data->get(\$name, \$namespace);\n\t\t\t\t\t\t\tif(\$oldValue != \$dbValue) {\n\t\t\t\t\t\t\t\t\$validUpdate = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\$newValue = null;\n\t\t\t\t\t\tif (\$namespace)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (isset (\$valuesToChangeTo[\$namespace][\$name]))\n\t\t\t\t\t\t\t\t\$newValue = \$valuesToChangeTo[\$namespace][\$name];\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{ \n\t\t\t\t\t\t\t\$newValue = \$valuesToChangeTo[\$name];\n\t\t\t\t\t\t}\n\t\t\n\t\t\t\t\t\tif (is_null(\$newValue)) {\n\t\t\t\t\t\t\t\$this->removeFromCustomData(\$name, \$namespace);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\$this->putInCustomData(\$name, \$newValue, \$namespace);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n                   \n\t\t\t\tif(!\$validUpdate) \n\t\t\t\t\tbreak;\n\t\t\t\t\t                   \n\t\t\t\t\$this->setCustomData(\$this->m_custom_data->toString());\n\t\t\t}\n\n\t\t\tif (\$isInsert) {\n\t\t\t\t\$this->postInsert(\$con);";
         $this->applyBehaviorModifier('postInsert', $script, "\t\t\t\t\t");
         $script .= "\n\t\t\t} else {\n\t\t\t\t\$this->postUpdate(\$con);";
         $this->applyBehaviorModifier('postUpdate', $script, "\t\t\t\t\t");
         $script .= "\n\t\t\t}\n\t\t\t\$this->postSave(\$con);";
         $this->applyBehaviorModifier('postSave', $script, "\t\t\t\t");
         $script .= "\n\t\t\t" . $this->getPeerClassname() . "::addInstanceToPool(\$this);\n\t\t\t\n\t\t\t\$con->commit();\n\t\t\treturn \$affectedRows;";
     } else {
         // save without runtime hooks
         $this->applyBehaviorModifier('preSave', $script, "\t\t\t");
         if ($this->hasBehaviorModifier('preUpdate')) {
             $script .= "\n\t\t\tif(!\$isInsert) {";
             $this->applyBehaviorModifier('preUpdate', $script, "\t\t\t\t");
             $script .= "\n\t\t\t}";
         }
         if ($this->hasBehaviorModifier('preInsert')) {
             $script .= "\n\t\t\tif(\$isInsert) {";
             $this->applyBehaviorModifier('preInsert', $script, "\t\t\t\t");
             $script .= "\n\t\t\t}";
         }
         $script .= "\n\t\t\t\$affectedRows = \$this->doSave(\$con" . ($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload" : "") . ");";
         $this->applyBehaviorModifier('postSave', $script, "\t\t\t");
         if ($this->hasBehaviorModifier('postUpdate')) {
             $script .= "\n\t\t\tif(!\$isInsert) {";
             $this->applyBehaviorModifier('postUpdate', $script, "\t\t\t\t");
             $script .= "\n\t\t\t}";
         }
         if ($this->hasBehaviorModifier('postInsert')) {
             $script .= "\n\t\t\tif(\$isInsert) {";
             $this->applyBehaviorModifier('postInsert', $script, "\t\t\t\t");
             $script .= "\n\t\t\t}";
         }
         $script .= "\n\t\t\t\$con->commit();\n\t\t\t" . $this->getPeerClassname() . "::addInstanceToPool(\$this);\n\t\t\treturn \$affectedRows;";
     }
     $script .= "\n\t\t} catch (PropelException \$e) {\n\t\t\t\$con->rollBack();\n\t\t\tthrow \$e;\n\t\t}";
 }