Permalink
Browse files

Moved some more code to twig

  • Loading branch information...
1 parent 8d4023f commit 962d44d4b599fea82b74a31e8c1abb9e293a2bd5 @mpscholten committed Oct 27, 2013
@@ -0,0 +1,52 @@
+/**
+ * Resets all references to other model objects or collections of model objects.
+ *
+ * This method is a user-space workaround for PHP's inability to garbage collect
+ * objects with circular references (even in PHP 5.3). This is currently necessary
+ * when using Propel in certain daemon or large-volume/high-memory operations.
+ *
+ * @param boolean $deep Whether to also clear the references on all referrer objects.
+ */
+public function clearAllReferences($deep = false)
+{
+ {% set vars = [] %}
+ if ($deep) {
+ {% for refFk in builder.table.referrers %}
+ {% if refFk.primaryKey %}
+ {% set varName = builder.pKRefFKVarName(refFk) %}
+ {% set vars = vars|merge([varName]) %}
+ if ($this->{{ varName }}) {
+ $this->{{ varName }}->clearAllReferences($deep);
+ }
+ {% else %}
+ {% set varName = builder.refFKCollVarName(refFk) %}
+ {% set vars = vars|merge([varName]) %}
+ if ($this->{{ varName }}) {
+ foreach ($this->{{ varName }} as $o) {
+ $o->clearAllReferences($deep);
+ }
+ }
+ {% endif %}
+ {% endfor %}
+
+ {% for fkList in builder.table.crossFks %}
+ {% set varName = builder.crossFKVarName(fkList[1]) %}
+ if ($this->{{ varName }}) {
+ foreach ($this->{{ varName }} as $o) {
+ $o->clearAllReferences($deep);
+ }
+ {% endfor %}
+ }
+
+ {# Todo: apply objectClearReferences #}
+ {% for varName in vars %}
+ if ($this->{{ varName }} instanceof Collection) {
+ $this->{{ varName }}->clearIterator();
+ }
+ $this->{{ varName }} = null;"
+ {% endfor %}
+
+ {% for foreignKey in builder.table.foreignKeys %}
+ $this->{{ builder.fKVarName(foreignKey) }} = null;";
+ {% endfor %}
+}
@@ -0,0 +1,15 @@
+{% for fkList in builder.table.crossFks %}
+ {% set refFk = fkList[0] %}
+ {% set crossFk = fkList[1] %}
+
+ {% set relCol = builder.fKPhpNameAffix(crossFk, true) %}
+ {% set collName = builder.crossFKVarName(crossFk) %}
+ {#
+
+ $this->declareClassFromBuilder($this->getNewStubObjectBuilder($crossFK->getForeignTable()), 'Child');
+ $this->declareClassFromBuilder($this->getNewStubQueryBuilder($crossFK->getForeignTable()));
+ #}
+ {% include 'Object/_crossFk/_methods/_clear.php.twig' %}
+ {% include 'Object/_crossFk/_methods/_init.php.twig' %}
+ {% include 'Object/_crossFk/_methods/_accessor.php.twig' %}
+{% endfor %}
@@ -0,0 +1,40 @@
+{% set relatedName = builder.fKPhpNameAffix(crossFk, true) %}
+{% set relatedObjectClassName = builder.classNameFromBuilder(builder.newStubObjectBuilder(crossFk.foreignTable)) %}
+{% set selfRelationName = builder.fKPhpNameAffix(refFk, false) %}
+{% set relatedQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(crossFk.foreignTable)) %}
+{% set crossRefTableName = crossFk.tableName %}
+
+/**
+ * Gets a collection of {{ relatedObjectClassName }} objects related by a many-to-many relationship
+ * to the current object by way of the $crossRefTableName cross-reference table.
+ *
+ * If the $criteria is not null, it is used to always fetch the results from the database.
+ * Otherwise the results are fetched from the database the first time, then cached.
+ * Next time the same method is called without $criteria, the cached collection is returned.
+ * If this ".$this->getObjectClassName()." is new, it will return
+ * an empty collection or the current collection; the criteria is ignored on a new object.
+ *
+ * @param Criteria $criteria Optional query object to filter the query
+ * @param ConnectionInterface $con Optional connection object
+ *
+ * @return ObjectCollection|{{ relatedObjectClassName }}[] List of {{ relatedObjectClassName }} objects
+ */
+public function get{{ relatedName }}($criteria = null, ConnectionInterface $con = null)
+{
+ if (null === $this->{{ collName }} || null !== $criteria) {
+ if ($this->isNew() && null === $this->{{ collName }}) {
+ // return empty collection
+ $this->init{{ relatedName }}}();
+ } else {
+ ${{ collName }} = {{ relatedQueryClassName }}::create(null, $criteria)
+ ->filterBy{{ selfRelationName }}($this)
+ ->find($con);
+ if (null !== $criteria) {
+ return ${{ collName }};
+ }
+ $this->{{ collName }} = ${{ collName }};
+ }
+ }
+
+ return $this->{{ collName }};
+}
@@ -0,0 +1 @@
+{{ builder.addCrossFKAdd(refFk, crossFk) }}
@@ -0,0 +1,14 @@
+/**
+ * Clears out the {{ collName }} collection
+ *
+ * This does not modify the database; however, it will remove any associated objects, causing
+ * them to be refetched by subsequent calls to accessor method.
+ *
+ * @return void
+ * @see add{{ relCol }}()
+ */
+public function clear{{ relCol }}()
+{
+ $this->{{ collName }} = null; // important to set this to NULL since that means it is uninitialized
+ $this->{{ collName }}Partial = null;
+}
@@ -0,0 +1,36 @@
+{% set relatedName = builder.getFKPhpNameAffix(crossFk, false) %}
+{% set relatedObjectClassName = builder.newStubObjectBuilder(crossFk.foreignTable).unqualifiedClassName %}
+{% set selfRelationName = builder.fKPhpNameAffix(refFk, false) %}
+{% set relatedQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(crossFk.foreignTable)) %}
+{% set crossRefTableName = crossFk.tableName %}
+{% set collName = builder.crossFKVarName(crossFk) %}
+
+/**
+* Gets the number of {{ relatedObjectClassName }} objects related by a many-to-many relationship
+* to the current object by way of the $crossRefTableName cross-reference table.
+*
+* @param Criteria $criteria Optional query object to filter the query
+* @param boolean $distinct Set to true to force count distinct
+* @param ConnectionInterface $con Optional connection object
+*
+* @return int the number of related {{ relatedObjectClassName }} objects
+*/
+public function count{{ relatedName }}($criteria = null, $distinct = false, ConnectionInterface $con = null)
+{
+ if (null === $this->{{ collName }} || null !== $criteria) {
+ if ($this->isNew() && null === $this->{{ collName }}) {
+ return 0;
+ } else {
+ $query = {{ relatedQueryClassName }}::create(null, $criteria);
+ if ($distinct) {
+ $query->distinct();
+ }
+
+ return $query
+ ->filterBy{{ selfRelationName }}($this)
+ ->count($con);
+ }
+ } else {
+ return count($this->{{ collName }});
+ }
+}
@@ -0,0 +1 @@
+{{ builder.addCrossFKDoAdd(refFk, crossFk) }}
@@ -0,0 +1,15 @@
+{% set relatedObjectClassName = builder.classNameFromBuilder(builder.newStubObjectBuilder(crossFk.foreignTable), true) %}
+/**
+ * Initializes the {{ collName }} collection.
+ *
+ * By default this just sets the $collName collection to an empty collection (like clear{{ relCol }}());
+ * however, you may wish to override this method in your stub class to provide setting appropriate
+ * to your application -- for example, setting the initial array to the values stored in database.
+ *
+ * @return void
+ */
+public function init{{ relCol }}()
+{
+ $this->{{ collName }} = new ObjectCollection();
+ $this->{{ collName }}->setModel('{{ relateObjectClassName }}');
+}
@@ -0,0 +1,34 @@
+{% set relatedNamePlural = builder.fKPhpNameAffix(crossFk, true) %}
+{% set relatedName = builder.getFKPhpNameAffix(crossFk, false) %}
+{% set relatedObjectClassName = builder.newStubObjectBuilder(crossFk.foreignTable).unqualifiedClassName %}
+{% set crossRefTableName = crossFk.tableName %}
+{% set collName = builder.crossFKVarName(crossFk) %}
+{% set inputCollection = relatedNamePlural|lcfirst %}
+{% set inputCollectionEntry = builder.fKPhpNameAffix(crossFk, false)|lcfirst %}
+/**
+ * Sets a collection of {{ relatedObjectClassName }} objects related by a many-to-many relationship
+ * to the current object by way of the {{ crossRefTableName }} cross-reference table.
+ * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
+ * and new objects from the given Propel collection.
+ *
+ * @param Collection ${{ inputCollection }} A Propel collection.
+ * @param ConnectionInterface $con Optional connection object
+ * @return {{ builder.objectClassName }} The current object (for fluent API support)
+ */
+public function set{{ relatedNamePlural }}(Collection ${{ inputCollection }}, ConnectionInterface $con = null)
+{
+ $this->clear{{ relatedNamePlural }}();
+ $current{{ relatedNamePlural }} = $this->get{{ relatedNamePlural }}();
+
+ $this->{{ inputCollection }}ScheduledForDeletion = $current{{ relatedNamePlural }}->diff(${{ inputCollection }});
+
+ foreach (${{ inputCollection }} as ${{ inputCollectionEntry }}) {
+ if (!$current{{ relatedNamePlural }}->contains(${$inputCollectionEntry})) {
+ $this->doAdd{{ relatedName }}(${$inputCollectionEntry});
+ }
+ }
+
+ $this->{{ collName }} = ${{ inputCollection }};
+
+ return $this;
+}
@@ -0,0 +1 @@
+{{ builder.addCrossFKRemove(refFk, crossFk) }}
@@ -1 +1,15 @@
-{{ builder.addDoUpdate }}
+/**
+ * Update the row in the database.
+ *
+ * @param ConnectionInterface $con
+ *
+ * @return Integer Number of updated rows
+ * @see doSave()
+ */
+protected function doUpdate(ConnectionInterface $con)
+{
+ $selectCriteria = $this->buildPkeyCriteria();
+ $valuesCriteria = $this->buildCriteria();
+
+ return $selectCriteria->doUpdate($valuesCriteria, $con);
+}

0 comments on commit 962d44d

Please sign in to comment.