Skip to content
Browse files

moved save and reload

  • Loading branch information...
1 parent 9777ca2 commit c4c0037f3723f8f2ae2d3925b45db23fa3f9f1d6 @mpscholten committed Oct 8, 2013
View
17 src/Propel/Generator/Builder/Om/templates/Object/_genericAccessors/_getByName.php.twig
@@ -0,0 +1,17 @@
+/**
+ * Retrieves a field from the object by name passed in as a string.
+ *
+ * @param string $name name
+ * @param string $type The type of fieldname the $name is of:
+ * one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME
+ * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
+ * Defaults to TableMap::{{ builder.defaultKeyType }}.
+ * @return mixed Value of field.
+ */
+public function getByName($name, $type = TableMap::{{ builder.defaultKeyType }})
+{
+ $pos = {{ builder.tableMapClass }}::translateFieldName($name, $type, TableMap::TYPE_NUM);
+ $field = $this->getByPosition($pos);
+
+ return $field;
+}
View
20 src/Propel/Generator/Builder/Om/templates/Object/_genericAccessors/_getByPosition.php.twig
@@ -0,0 +1,20 @@
+/**
+ * Retrieves a field from the object by Position as specified in the xml schema.
+ * Zero-based.
+ *
+ * @param int $pos position in xml schema
+ * @return mixed Value of field at $pos
+ */
+public function getByPosition($pos)
+{
+ switch ($pos) {
+ {% for column in builder.table.columns %}
+ case {{ loop.index0 }}:
+ return $this->get{{ column.phpName }}();
+ break;
+ {% endfor %}
+ default:
+ return null;
+ break;
+ }
+}
View
64 src/Propel/Generator/Builder/Om/templates/Object/_genericAccessors/_toArray.php.twig
@@ -0,0 +1,64 @@
+{% set hasFks = (builder.table.foreignKeys|length > 0) and (builder.table.referrers|length > 0) %}
+/**
+ * Exports the object as an array.
+ *
+ * You can specify the key type of the array by passing one of the class
+ * type constants.
+ *
+ * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME,
+ * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
+ * Defaults to TableMap::{{ builder.defaultKeyType }}.
+ * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
+ * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion"
+{% if hasFks %}
+ * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
+{% endif %}
+ *
+ * @return array an associative array containing the field names (as keys) and field values
+ */
+public function toArray($keyType = TableMap::{{ builder.defaultKeyType }}, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(){% if hasFks %}, $includeForeignObjects = false{% endif %})
+{
+ if (isset($alreadyDumpedObjects['{{ builder.unqualifiedClassName }}'][{% if builder.table.hasCompositePrimaryKey %}serialize($this->getPrimaryKey()){% else %}$this->getPrimaryKey(){% endif %}])) {
+ return '*RECURSION*';
+ }
+ $alreadyDumpedObjects['{{ builder.unqualifiedClassName }}'][{% if builder.table.hasCompositePrimaryKey %}serialize($this->getPrimaryKey()){% else %}$this->getPrimaryKey(){% endif %}] = true;
+ $keys = {{ builder.tableMapClassName }}::getFieldNames($keyType);
+
+ $result = array(
+ {% for column in builder.table.columns %}
+ {% if column.lazyLoad %}
+ $keys[{{ loop.index0 }}] => ($includeLazyLoadColumns) ? $this->get{{ column.phpName }}() : null,
+ {% else %}
+ $keys[{{ loop.index0 }}] => $this->get{{ column.phpName }}(),
+ {% endif %}
+ {% endfor %}
+ );
+
+ $virtualColumns = $this->virtualColumns;
+ foreach ($virtualColumns as $key => $virtualColumn) {
+ $result[$key] = $virtualColumn;
+ }
+
+ {% if hasFks %}
+ if ($includeForeignObjects) {
+ {% for foreignKey in builder.table.foreignKeys %}
+ if (null !== $this->{{ builder.fKVarName(foreignKey) }}) {
+ $result['{{ builder.fKPhpNameAffix(foreignKey, false) }}'] = $this->{{ builder.fKVarName(foreignKey) }}->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true);
+ }
+ {% endfor %}
+ {% for ref in builder.table.referrers %}
+ {% if ref.localPrimaryKey %}
+ if (null !== $this->{{ builder.pKRefFKVarName(ref) }}) {
+ $result['{{ builder.refFKPhpNameAffix(ref, false) }}'] = $this->{{ builder.pKRefFKVarName(ref) }}->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true);
+ }
+ {% else %}
+ if (null !== $this->{{ builder.refFKCollVarName(ref) }}) {
+ $result['{{ builder.refFKPhpNameAffix(ref, true) }}'] = $this->{{ builder.refFKCollVarName(ref) }}->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
+ }
+ {% endif %}
+ {% endfor %}
+ }
+ {% endif %}
+
+ return $result;
+}
View
92 src/Propel/Generator/Builder/Om/templates/Object/_manipulationMethods/_reload.php.twig
@@ -12,50 +12,50 @@ public function reload($deep = false, ConnectionInterface $con = null)
{
if ($this->isDeleted()) {
throw new PropelException("Cannot reload a deleted object.");
-}
-
-if ($this->isNew()) {
- throw new PropelException("Cannot reload an unsaved object.");
-}
-
-if ($con === null) {
- $con = Propel::getServiceContainer()->getReadConnection({{ builder.tableMapClass }}::DATABASE_NAME);
-}
-
-// We don't need to alter the object instance pool; we're just modifying this instance
-// already in the pool.
-
-$dataFetcher = {{ builder.queryClassName }}::create(null, $this->buildPkeyCriteria())->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con);
-$row = $dataFetcher->fetch();
-$dataFetcher->close();
-if (!$row) {
- throw new PropelException('Cannot find matching row in the database to reload object values.');
-}
-$this->hydrate($row, 0, true, $dataFetcher->getIndexType()); // rehydrate
-
-{% for column in builder.table.columns %}
- {% if column.lazyLoad %}
- // Reset the $clo lazy-load column
- $this->{{ column.name|lower }} = null;
- $this->{{ column.name|lower }}_isLoaded = false;
- {% endif %}
-{% endfor %}
-
-if ($deep) { // also de-associate any related objects?
-
-{% for fk in builder.table.foreignKeys %}
- $this->{{ builder.fkVarName(fk) }} = null;
-{% endfor %}
-
-{% for refFk in builder.table.referrers %}
- {% if refFk.localPrimaryKey %}
- $this->{{ builder.pKRefFKVarName(refFk) }} = null;
- {% else %}
- $this->{{ builder.refFKCollVarName(refFk) }} = null;
- {% endif %}
-{% endfor %}
-
-{% for fkList in builder.table.crossFk %}
- $this->{{ builder.crossFKVarName(fkList[1]) }} = null;
-{% endfor %}
+ }
+
+ if ($this->isNew()) {
+ throw new PropelException("Cannot reload an unsaved object.");
+ }
+
+ if ($con === null) {
+ $con = Propel::getServiceContainer()->getReadConnection({{ builder.tableMapClass }}::DATABASE_NAME);
+ }
+
+ // We don't need to alter the object instance pool; we're just modifying this instance
+ // already in the pool.
+
+ $dataFetcher = {{ builder.queryClassName }}::create(null, $this->buildPkeyCriteria())->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con);
+ $row = $dataFetcher->fetch();
+ $dataFetcher->close();
+ if (!$row) {
+ throw new PropelException('Cannot find matching row in the database to reload object values.');
+ }
+ $this->hydrate($row, 0, true, $dataFetcher->getIndexType()); // rehydrate
+
+ {% for column in builder.table.columns %}
+ {% if column.lazyLoad %}
+ // Reset the $clo lazy-load column
+ $this->{{ column.name|lower }} = null;
+ $this->{{ column.name|lower }}_isLoaded = false;
+ {% endif %}
+ {% endfor %}
+
+ if ($deep) { // also de-associate any related objects?
+ {% for fk in builder.table.foreignKeys %}
+ $this->{{ builder.fkVarName(fk) }} = null;
+ {% endfor %}
+
+ {% for refFk in builder.table.referrers %}
+ {% if refFk.localPrimaryKey %}
+ $this->{{ builder.pKRefFKVarName(refFk) }} = null;
+ {% else %}
+ $this->{{ builder.refFKCollVarName(refFk) }} = null;
+ {% endif %}
+ {% endfor %}
+
+ {% for fkList in builder.table.crossFks %}
+ $this->{{ builder.crossFKVarName(fkList[1]) }} = null;
+ {% endfor %}
+ }
}
View
86 src/Propel/Generator/Builder/Om/templates/Object/_manipulationMethods/_save.php.twig
@@ -1 +1,85 @@
-{{ builder.addSave }}
+/**
+ * Persists this object to the database.
+ *
+ * If the object is new, it inserts it; otherwise an update is performed.
+ * All modified related objects will also be persisted in the doSave()
+ * method. This method wraps all precipitate database operations in a
+ * single transaction.
+
+{% if builder.table.reloadOnUpdate %}
+ *
+ * Since this table was configured to reload rows on update, the object will
+ * be reloaded from the database if an UPDATE operation is performed (unless
+ * the $skipReload parameter is TRUE).
+{% endif %}
+
+{% if builder.table.reloadOnInsert %}
+ *
+ * Since this table was configured to reload rows on insert, the object will
+ * be reloaded from the database if an INSERT operation is performed (unless
+ * the $skipReload parameter is TRUE).
+{% endif %}
+
+*
+* @param ConnectionInterface $con
+
+{% if builder.table.reloadOnUpdate or builder.table.reloadOnInsert %}
+ * @param boolean $skipReload Whether to skip the reload for this object from database.
+{% endif %}
+ * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
+ * @throws PropelException
+ * @see doSave()
+ */
+public function save(ConnectionInterface $con = null{% if builder.table.reloadOnUpdate or builder.table.reloadOnInsert %}, $skipReload = false{% endif %})
+{
+ if ($this->isDeleted()) {
+ throw new PropelException("You cannot save an object that has been deleted.");
+ }
+
+ if ($con === null) {
+ $con = Propel::getServiceContainer()->getWriteConnection({{ builder.tableMapClass }}::DATABASE_NAME);
+ }
+
+ $con->beginTransaction();
+ $isInsert = $this->isNew();
+ {# TODO: why not replace those pre/post-whatever hooks with a behavior? would be much cleaner code :) #}
+ try {
+ {% set addHooks = builder.generatorConfig.buildProperty('addHooks') %}
+ {% if addHooks %}$ret = $this->preSave($con);{% endif %}
+ {{ behavior_api.include('Object/_hooks/_preSave.php.twig', builder) }}
+
+ if ($isInsert) {
+ {% if addHooks %}$ret = $ret && $this->preInsert($con);{% endif %}
+ {{ behavior_api.include('Object/_hooks/_preInsert.php.twig', builder) }}
+ } else {
+ {% if addHooks %}$ret = $ret && $this->preUpdate($con);{% endif %}
+ {{ behavior_api.include('Object/_hooks/_preUpdate.php.twig', builder) }}
+ }
+
+ $affectedRows = 0;
+ {% if addHooks %}
+ if($ret) {
+ {% endif %}
+ $affectedRows = $this->doSave($con {% if builder.table.reloadOnUpdate or builder.table.reloadOnInsert %}, $skipReload{% endif %});
+ if ($isInsert) {
+ {% if addHooks %}$this->postInsert($con);{% endif %}
+ {{ behavior_api.include('Object/_hooks/_postInsert.php.twig', builder) }}
+ } else {
+ {% if addHooks %}$this->postUpdate($con);{% endif %}
+ {{ behavior_api.include('Object/_hooks/_postUpdate.php.twig', builder) }}
+ }
+
+ {% if addHooks %}$this->postSave($con);{% endif %}
+ {{ behavior_api.include('Object/_hooks/_postSave.php.twig', builder) }}
+ {{ builder.tableMapClass }}::addInstanceToPool($this);
+ {% if addHooks %}
+ }
+ {% endif %}
+ $con->commit();
+ return $affectedRows;
+ } catch(Exception $e) {
+ $con->rollBack();
+ throw $e;
+ }
+
+}
View
5 src/Propel/Generator/Builder/Om/templates/behavior_include.twig
@@ -0,0 +1,5 @@
+{% macro include(template, builder) %}
+ {% for behavior in builder.table.behaviors %}
+ {{ include('@' ~ behavior.templateNamespace ~ '/' ~ template, {'builder': builder, 'behavior': behavior}, ignore_missing = true, with_context = false) }}
+ {% endfor %}
+{% endmacro %}

0 comments on commit c4c0037

Please sign in to comment.
Something went wrong with that request. Please try again.