Permalink
Browse files

[Versionable] Added template for isVersioningNecessary()

  • Loading branch information...
1 parent 7de61da commit ba385f649fed1ce8c8f1ab2e6b2ec44b8da0c07a @willdurand willdurand committed Jun 11, 2012
@@ -211,57 +211,23 @@ public function enforceVersioning()
protected function addIsVersioningNecessary(&$script)
{
- $peerClass = $this->builder->getStubPeerBuilder()->getClassname();
- $script .= "
-/**
- * Checks whether the current state must be recorded as a version
- *
- * @param PropelPDO \$con An optional PropelPDO connection to use.
- *
- * @return boolean
- */
-public function isVersioningNecessary(\$con = null)
-{
- if (\$this->alreadyInSave) {
- return false;
- }
-
- if (\$this->enforceVersion) {
- return true;
- }
+ $peerClassName = $this->builder->getStubPeerBuilder()->getClassname();
- if ({$peerClass}::isVersioningEnabled() && (\$this->isNew() || \$this->isModified() || \$this->isDeleted())) {
- return true;
- }";
+ $fkGetters = array();
foreach ($this->behavior->getVersionableFks() as $fk) {
- $fkGetter = $this->builder->getFKPhpNameAffix($fk, $plural = false);
- $script .= "
-
- if (null !== (\$object = \$this->get{$fkGetter}(\$con)) && \$object->isVersioningNecessary(\$con)) {
- return true;
- }";
+ $fkGetters[] = $this->builder->getFKPhpNameAffix($fk, $plural = false);
}
- foreach ($this->behavior->getVersionableReferrers() as $fk) {
- $fkGetter = $this->builder->getRefFKPhpNameAffix($fk, $plural = true);
- $script .= "
- // to avoid infinite loops, emulate in save
- \$this->alreadyInSave = true;
- foreach (\$this->get{$fkGetter}(null, \$con) as \$relatedObject) {
- if (\$relatedObject->isVersioningNecessary(\$con)) {
- \$this->alreadyInSave = false;
- return true;
- }
- }
- \$this->alreadyInSave = false;
-";
+ $refFkGetters = array();
+ foreach ($this->behavior->getVersionableReferrers() as $fk) {
+ $refFkGetters[] = $this->builder->getRefFKPhpNameAffix($fk, $plural = true);
}
- $script .= "
-
- return false;
-}
-";
+ $script .= $this->behavior->renderTemplate('objectIsVersioningNecessary', array(
+ 'peerClassName' => $peerClassName,
+ 'fkGetters' => $fkGetters,
+ 'refFkGetters' => $refFkGetters,
+ ));
}
protected function addAddVersion(&$script)
@@ -0,0 +1,41 @@
+
+/**
+ * Checks whether the current state must be recorded as a version
+ *
+ * @return boolean
+ */
+public function isVersioningNecessary($con = null)
+{
+ if ($this->alreadyInSave) {
+ return false;
+ }
+
+ if ($this->enforceVersion) {
+ return true;
+ }
+
+ if (<?php echo $peerClassName ?>::isVersioningEnabled() && ($this->isNew() || $this->isModified() || $this->isDeleted())) {
+ return true;
+ }
+
+<?php foreach ($fkGetters as $fkGetter) : ?>
+ if (null !== ($object = $this->get<?php echo $fkGetter ?>($con)) && $object->isVersioningNecessary($con)) {
+ return true;
+ }
+
+<?php endforeach; ?>
+<?php foreach ($refFkGetters as $fkGetter) : ?>
+ // to avoid infinite loops, emulate in save
+ $this->alreadyInSave = true;
+ foreach ($this->get<?php echo $fkGetter ?>(null, $con) as $relatedObject) {
+ if ($relatedObject->isVersioningNecessary($con)) {
+ $this->alreadyInSave = false;
+
+ return true;
+ }
+ }
+ $this->alreadyInSave = false;
+
+<?php endforeach; ?>
+ return false;
+}

0 comments on commit ba385f6

Please sign in to comment.