Permalink
Browse files

moved fk methods

  • Loading branch information...
1 parent bfba5e9 commit f2f29ecf964f4c033032ecaf71a6e90384072886 @mpscholten committed Oct 13, 2013
@@ -0,0 +1,26 @@
+{% set referrers = builder.table.referrers %}
+
+{% for refFk in referrers %}
+ {% if loop.first %}
+ {% include 'Object/_refFk/_methods/_initRelation.php.twig' %}
+ {% endif %}
+
+ {% do builder.declareClassFromBuilder(builder.newStubObjectBuilder(refFk.table), 'Child') %}
+ {% do builder.declareClassFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+
+ {% if refFk.localPrimaryKey %}
+ {% include 'Object/_refFk/_methods/_getPkRefFk.php.twig' %}
+ {% include 'Object/_refFk/_methods/_setPkRefFk.php.twig' %}
+ {% else %}
+ {% include 'Object/_refFk/_methods/_refFkClear.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkPartial.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkInit.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkGet.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkSet.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkCount.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkAdd.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkDoAdd.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkRemove.php.twig' %}
+ {% include 'Object/_refFk/_methods/_refFkGetJoinMethods.php.twig' %}
+ {% endif %}
+{% endfor %}
@@ -0,0 +1,5 @@
+{% set relCol = builder.refFKPhpNameAffix(refFk, true) %}
+{% set collVarName = builder.refFKCollVarName(refFk) %}
+
+{% block method %}
+{% endblock %}
@@ -0,0 +1,27 @@
+{#
+ Adds the method that gets a one-to-one related referrer fkey.
+ This is for one-to-one relationship special case.
+#}
+
+{% set joinedTableObjectBuilder = builder.newObjectBuilder(refFk.table) %}
+{% set className = joinedTableObjectBuilder.objectClassName %}
+
+{% set queryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+
+{% set varName = builder.pKRefFKVarName(refFk) %}
+
+/**
+ * Gets a single {{ className }} object, which is related to this object by a one-to-one relationship.
+ *
+ * @param ConnectionInterface $con optional connection object
+ * @return {{ className }}
+ * @throws PropelException
+ */
+public function get{{ builder.refFKPhpNameAffix(refFk, false) }}(ConnectionInterface $con = null)
+{
+ if ($this->{{ varName }} === null && $this->isNew()) {
+ $this->{{ varName }} = {{ queryClassName }}::create()->findPk($this->getPrimaryKey(), $con);
+ }
+
+ return $this->{{ varName }};
+}
@@ -0,0 +1,18 @@
+/**
+ * Initializes a collection based on the name of a relation.
+ * Avoids crafting an 'init[$relationName]s' method name
+ * that wouldn't work when StandardEnglishPluralizer is used.
+ *
+ * @param string $relationName The name of the relation to initialize
+ * @return void
+ */
+public function initRelation($relationName)
+{
+ {% for refFk in referrers if refFk.localPrimaryKey %}
+ {% set relationName = builder.refFKPhpNameAffix(refFk) %}
+ {% set relCol = builder.refFKPhpNameAffix(refFk, true) %}
+ if ('{{ relationName }}' == $relationName) {
+ return $this->init{{ relCol }}();
+ }
+ {% endfor %}
+}
@@ -0,0 +1,29 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ {% set joinedTableObjectBuilder = builder.newObjectBuilder(refFk.table) %}
+ {% set className = joinedTableObjectBuilder.className %}
+ {% set fkQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+
+ /**
+ * Method called to associate a {{ className }} object to this object
+ * through the {{ className }} foreign key attribute.
+ *
+ * @param {{ className }} $l {{ className }}
+ * @return {{ builder.objectClassName(true) }} The current object (for fluent API support)
+ */
+ public function add{{ builder.refFkPhpNameAffix(refFk, false) }}({{ className }} $l)
+ {
+ if ($this->{{ collVarName }} === null) {
+ $this->init{{ relCol }}();
+ $this->{{ collVarName }}Partial = true;
+ }
+
+ if (!in_array($l, $this->{{ collVarName }}->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
+ $this->doAdd{{ builder.refFkPhpNameAffix(refFk, false) }}($l);
+ }
+
+ return $this;
+ }
+{% endblock %}
@@ -0,0 +1,18 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ /**
+ * Clears out the {{ collVarName }} 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->{{ collVarName }} = null; // important to set this to NULL since that means it is uninitialized
+ }
+{% endblock %}
@@ -0,0 +1,42 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ {% set joinedTableObjectBuilder = builder.newObjectBuilder(refFk.table) %}
+ {% set className = joinedTableObjectBuilder.className %}
+ {% set fkQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+
+ /**
+ * Returns the number of related {{ className }} objects.
+ *
+ * @param Criteria $criteria
+ * @param boolean $distinct
+ * @param ConnectionInterface $con
+ * @return int Count of related {{ className }} objects.
+ * @throws PropelException
+ */
+ public function count{{ relCol }}(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null)
+ {
+ $partial = $this->{{ collVarName }}Partial && !$this->isNew();
+ if (null === $this->{{ collVarName }} || null !== $criteria || $partial) {
+ if ($this->isNew() && null === $this->{{ collVarName }}) {
+ return 0;
+ }
+
+ if ($partial && !$criteria) {
+ return count($this->get{{ relCol }}());
+ }
+
+ $query = {{ fkQueryClassName }}::create(null, $criteria);
+ if ($distinct) {
+ $query->distinct();
+ }
+
+ return $query
+ ->filterBy{{ builder.fKPhpNameAffix(refFk) }}($this)
+ ->count($con);
+ }
+
+ return count($this->{{ collVarName }});
+ }
+{% endblock %}
@@ -0,0 +1,18 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ {% set joinedTableObjectBuilder = builder.newObjectBuilder(refFk.table) %}
+ {% set className = joinedTableObjectBuilder.className %}
+ {% set fkQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+ {% set relatedObjectClassName = builder.refFKPhpNameAffix(refFk, false) %}
+
+ /**
+ * @param {{ relatedObjectClassName }} ${{ relCol|lcfirst }} The {{ relCol|lcfirst }} object to add.
+ */
+ protected function doAdd{{ relatedObjectClassName }}({{ relatedObjectClassName }} ${{ relatedObjectClassName|lcfirst }})
+ {
+ $this->{{ collVarName }}[]= ${{ relatedObjectClassName|lcfirst }};
+ ${{ relatedObjectClassName|lcfirst }}->set{{ builder.fKPhpNameAffix(refFk, false) }}($this);
+ }
+{% endblock %}
@@ -0,0 +1,68 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ {% set joinedTableObjectBuilder = builder.newObjectBuilder(refFk.table) %}
+ {% set className = joinedTableObjectBuilder.className %}
+ {% set fkQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+
+ /**
+ * Gets an array of {{ className }} objects which contain a foreign key that references this object.
+ *
+ * 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 {{ builder.objectClassName }} is new, it will return
+ * an empty collection or the current collection; the criteria is ignored on a new object.
+ *
+ * @param Criteria $criteria optional Criteria object to narrow the query
+ * @param ConnectionInterface $con optional connection object
+ * @return Collection|{{ className }}[] List of {{ className }} objects
+ * @throws PropelException
+ */
+ public function get{{ relCol }}($criteria = null, ConnectionInterface $con = null)
+ {
+ $partial = $this->{{ collVarName }}Partial && !$this->isNew();
+ if (null === $this->{{ collVarName }} || null !== $criteria || $partial) {
+ if ($this->isNew() && null === $this->{{ collVarName }}) {
+ // return empty collection
+ $this->init{{ builder.refFKPhpNameAffix(refFk, true) }}();
+ } else {
+ ${{ collVarName }} = {{ fkQueryClassName }}::create(null, $criteria)
+ ->filterBy{{ builder.fKPhpNameAffix(refFk) }}($this)
+ ->find($con);
+
+ if (null !== $criteria) {
+ if (false !== $this->{{ collVarName }}Partial && count(${{ collVarName }})) {
+ $this->init{{ builder.refFKPhpNameAffix(refFk, true) }}(false);
+
+ foreach (${{ collVarName }} as $obj) {
+ if (false == $this->{{ collVarName }}->contains($obj)) {
+ $this->{{ collVarName }}->append($obj);
+ }
+ }
+
+ $this->{{ collVarName }}Partial = true;
+ }
+
+ ${{ collVarName }}->getInternalIterator()->rewind();
+
+ return ${{ collVarName }};
+ }
+
+ if ($partial && $this->{{ collVarName }}) {
+ foreach ($this->{{ collVarName }} as $obj) {
+ if ($obj->isNew()) {
+ ${{ collVarName }}[] = $obj;
+ }
+ }
+ }
+
+ $this->{{ collVarName }} = ${{ collVarName }};
+ $this->{{ collVarName }}Partial = false;
+ }
+ }
+
+ return $this->{{ collVarName }};
+ }
+{% endblock %}
@@ -0,0 +1,53 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ {% set joinBehavior = builder.generatorConfig.buildProperty('useLeftJoinsInDoJoinMethods') ? 'Criteria::LEFT_JOIN' : 'Criteria::INNER_JOIN' %}
+
+ {% set fkQueryClassName = builder.classNameFromBuilder(builder.newStubQueryBuilder(refFk.table)) %}
+ {% set fkObjectBuilder = builder.newObjectBuilder(refFk.table) %}
+ {% set className = fkObjectBuilder.objectClassName %}
+
+ {% for foreignKey in refFk.table.foreignKeys %}
+ {% set foreignTable = foreignKey.foreignTable %}
+ {% set doJoinGet = not foreignTable.forReferenceOnly %}
+
+ {#
+ it doesn't make sense to join in rows from the current table, since we are fetching
+ objects related to *this* table (i.e. the joined rows will all be the same row as current object)
+ #}
+ {% if builder.table.phpName == foreignTable.phpName %}
+ {% set doJoinGet = false %}
+ {% endif %}
+
+ {% if builder.relatedBySuffix(refFk) != '' and builder.relatedBySuffix(refFk) == builder.relatedBySuffix(foreignKey) %}
+ {% set doJoinGet = false %}
+ {% endif %}
+
+ {% if doJoinGet %}
+ /**
+ * If this collection has already been initialized with
+ * an identical criteria, it returns the collection.
+ * Otherwise if this {{ builder.table.phpName }} is new, it will return
+ * an empty collection; or if this {{ builder.table.phpName }} has previously
+ * been saved, it will retrieve related {{ relCol }} from storage.
+ *
+ * This method is protected by default in order to keep the public
+ * api reasonable. You can provide public methods for those you
+ * actually need in {{ builder.table.phpName }}.
+ *
+ * @param Criteria $criteria optional Criteria object to narrow the query
+ * @param ConnectionInterface $con optional connection object
+ * @param string $joinBehavior optional join type to use (defaults to {{ joinBehavior }})
+ * @return Collection|{$className}[] List of $className objects
+ */
+ public function get{{ relCol }}Join{{ builder.fKPhpNameAffix(foreignKey, false) }}($criteria = null, $con = null, $joinBehavior = {{ joinBehavior }})
+ {
+ $query = {{ fkQueryClassName }}::create(null, $criteria);
+ $query->joinWith('{{ builder.fKPhpNameAffix(foreignKey, false) }}', $joinBehavior);
+
+ return $this->get{{ relCol }}($query, $con);
+ }
+ {% endif %}
+ {% endfor %}
+{% endblock %}
@@ -0,0 +1,25 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ /**
+ * Initializes the {{ collVarName }} collection.
+ *
+ * By default this just sets the {{ collVarName }} collection to an empty array (like clear{{ collVarName }}());
+ * 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.
+ *
+ * @param boolean $overrideExisting If set to true, the method call initializes
+ * the collection even if it is not empty
+ *
+ * @return void
+ */
+ public function init{{ relCol }}($overrideExisting = true)
+ {
+ if (null !== $this->{{ collVarName }} && !\overrideExisting) {
+ return;
+ }
+ $this->{{ collVarName }} = new ObjectCollection();
+ $this->{{ collVarName }}->setModel('{{ builder.classNameFromBuilder(builder.newStubObjectBuilder(refFk.table), true) }}');
+ }
+{% endblock %}
@@ -0,0 +1,12 @@
+{% extends 'Object/_refFk/_methods/_baseFkMethod.php.twig' %}
+
+{% block method %}
+ {{ parent() }}
+ /**
+ * Reset is the {{ collVarName }} collection loaded partially.
+ */
+ public function resetPartial{{ relCol }}($v = true)
+ {
+ $this->{{ collVarName }}Partial = $v;
+ }
+{% endblock %}
Oops, something went wrong.

0 comments on commit f2f29ec

Please sign in to comment.