Skip to content
Browse files

fixed some behavior problems

  • Loading branch information...
1 parent f2f29ec commit 84310baab303c6153d61268732d7dc83f5132bf8 @mpscholten committed Oct 13, 2013
View
110 src/Propel/Generator/Behavior/ConcreteInheritance/ConcreteInheritanceBehavior.php
@@ -115,7 +115,7 @@ public function modifyTable()
}
- protected function getParentTable()
+ public function getParentTable()
{
$database = $this->getTable()->getDatabase();
$tableName = $database->getTablePrefix() . $this->getParameter('extends');
@@ -126,7 +126,7 @@ protected function getParentTable()
return $database->getTable($tableName);
}
- protected function isCopyData()
+ public function isCopyData()
{
return 'true' === $this->getParameter('copy_data_to_parent');
}
@@ -143,110 +143,4 @@ public function parentClass($builder)
return null;
}
-
- public function preSave($script)
- {
- if ($this->isCopyData()) {
- return "\$parent = \$this->getSyncParent(\$con);
-\$parent->save(\$con);
-\$this->setPrimaryKey(\$parent->getPrimaryKey());
-";
- }
- }
-
- public function postDelete($script)
- {
- if ($this->isCopyData()) {
- return "\$this->getParentOrCreate(\$con)->delete(\$con);
-";
- }
- }
-
- public function objectMethods($builder)
- {
- if (!$this->isCopyData()) {
- return;
- }
- $this->builder = $builder;
- $script = '';
- $this->addObjectGetParentOrCreate($script);
- $this->addObjectGetSyncParent($script);
-
- return $script;
- }
-
- protected function addObjectGetParentOrCreate(&$script)
- {
- $parentTable = $this->getParentTable();
- $parentClass = $this->builder->getClassNameFromBuilder($this->builder->getNewStubObjectBuilder($parentTable));
- $script .= "
-/**
- * Get or Create the parent " . $parentClass . " object of the current object
- *
- * @return " . $parentClass . " The parent object
- */
-public function getParentOrCreate(\$con = null)
-{
- if (\$this->isNew()) {
- if (\$this->isPrimaryKeyNull()) {
- \$parent = new " . $parentClass . "();
- \$parent->set" . $this->getParentTable()->getColumn($this->getParameter('descendant_column'))->getPhpName() . "('" . $this->builder->getStubObjectBuilder()->getQualifiedClassName() . "');
-
- return \$parent;
- } else {
- \$parent = " . $this->builder->getNewStubQueryBuilder($parentTable)->getClassname() . "::create()->findPk(\$this->getPrimaryKey(), \$con);
- if (null === \$parent || null !== \$parent->getDescendantClass()) {
- \$parent = new " . $parentClass . "();
- \$parent->setPrimaryKey(\$this->getPrimaryKey());
- \$parent->set" . $this->getParentTable()->getColumn($this->getParameter('descendant_column'))->getPhpName() . "('" . $this->builder->getStubObjectBuilder()->getQualifiedClassName() . "');
- }
-
- return \$parent;
- }
- } else {
- return " . $this->builder->getClassNameFromBuilder($this->builder->getNewStubQueryBuilder($parentTable)) . "::create()->findPk(\$this->getPrimaryKey(), \$con);
- }
-}
-";
- }
-
- protected function addObjectGetSyncParent(&$script)
- {
- $parentTable = $this->getParentTable();
- $pkeys = $parentTable->getPrimaryKey();
- $cptype = $pkeys[0]->getPhpType();
- $script .= "
-/**
- * Create or Update the parent " . $parentTable->getPhpName() . " object
- * And return its primary key
- *
- * @return " . $cptype . " The primary key of the parent object
- */
-public function getSyncParent(\$con = null)
-{
- \$parent = \$this->getParentOrCreate(\$con);";
- foreach ($parentTable->getColumns() as $column) {
- if ($column->isPrimaryKey() || $column->getName() == $this->getParameter('descendant_column')) {
- continue;
- }
- $phpName = $column->getPhpName();
- $script .= "
- \$parent->set{$phpName}(\$this->get{$phpName}());";
- }
- foreach ($parentTable->getForeignKeys() as $fk) {
- if (isset($fk->isParentChild) && $fk->isParentChild) {
- continue;
- }
- $refPhpName = $this->builder->getFKPhpNameAffix($fk, false);
- $script .= "
- if (\$this->get" . $refPhpName . "() && \$this->get" . $refPhpName . "()->isNew()) {
- \$parent->set" . $refPhpName . "(\$this->get" . $refPhpName . "());
- }";
- }
- $script .= "
-
- return \$parent;
-}
-";
- }
}
View
50 src/Propel/Generator/Behavior/ConcreteInheritance/ConcreteInheritanceParentBehavior.php
@@ -38,55 +38,19 @@ public function modifyTable()
}
}
- protected function getColumnGetter()
+ public function getColumnGetter()
{
return 'get' . $this->getColumnForParameter('descendant_column')->getPhpName();
}
- public function objectMethods($builder)
+ public function getTemplateDirectory()
{
- $this->builder = $builder;
- $this->builder->declareClasses('Propel\Runtime\ActiveQuery\PropelQuery');
- $script = '';
- $this->addHasChildObject($script);
- $this->addGetChildObject($script);
+ $directory = parent::getTemplateDirectory();
- return $script;
- }
-
- protected function addHasChildObject(&$script)
- {
- $script .= "
-/**
- * Whether or not this object is the parent of a child object
- *
- * @return bool
- */
-public function hasChildObject()
-{
- return \$this->" . $this->getColumnGetter() . "() !== null;
-}
-";
- }
-
- protected function addGetChildObject(&$script)
- {
- $script .= "
-/**
- * Get the child object of this object
- *
- * @return mixed
- */
-public function getChildObject()
-{
- if (!\$this->hasChildObject()) {
- return null;
- }
- \$childObjectClass = \$this->" . $this->getColumnGetter() . "();
- \$childObject = PropelQuery::from(\$childObjectClass)->findPk(\$this->getPrimaryKey());
+ if($directory !== null) {
+ return $directory . '_parent';
+ }
- return \$childObject->hasChildObject() ? \$childObject->getChildObject() : \$childObject;
-}
-";
+ return $directory;
}
}
View
3 ...ropel/Generator/Behavior/ConcreteInheritance/templates/Object/_hooks/_postDelete.php.twig
@@ -0,0 +1,3 @@
+{% if behavior.copyData %}
+ $this->getParentOrCreate($con)->delete($con);
+{% endif %}
View
5 src/Propel/Generator/Behavior/ConcreteInheritance/templates/Object/_hooks/_preSave.php.twig
@@ -0,0 +1,5 @@
+{% if behavior.copyData %}
+ $parent = $this->getSyncParent($con);
+ $parent->save($con);
+ $this->setPrimaryKey($parent->getPrimaryKey());
+{% endif %}
View
57 src/Propel/Generator/Behavior/ConcreteInheritance/templates/Object/_methods.php.twig
@@ -0,0 +1,57 @@
+{% if behavior.copyData %}
+ {% set parentTable = behavior.parentTable %}
+ {% set parentClassName = builder.classNameFromBuilder(builder.newStubObjectBuilder(parentTable)) %}
+ {% set parentQueryClassName = builder.newStubQueryBuilder(parentTable).className %}
+
+ /**
+ * Get or Create the parent {{ parentClassName }} object of the current object
+ *
+ * @return {{ parentClassName }} The parent object
+ */
+ public function getParentOrCreate($con = null)
+ {
+ if ($this->isNew()) {
+ if ($this->isPrimaryKeyNull()) {
+ $parent = new {{ parentClassName }}();
+ $parent->set{{ parentTable.column(behavior.parameter('descendant_column')).phpName }}('{{ builder.stubObjectBuilder.qualifiedClassName }}');
+
+ return $parent;
+ } else {
+ $parent = {{ parentQueryClassName }}::create()->findPk($this->getPrimaryKey(), $con);
+ if (null === $parent || null !== $parent->getDescendantClass()) {
+ $parent = new {{ parentClassName }}();
+ $parent->setPrimaryKey($this->getPrimaryKey());
+ $parent->set{{ parentTable.column(behavior.parameter('descendant_column')).phpName }}('{{ builder.stubObjectBuilder.qualifiedClassName }}');
+ }
+
+ return $parent;
+ }
+ } else {
+ return {{ parentQueryClassName }}::create()->findPk($this->getPrimaryKey(), $con);
+ }
+ }
+
+ /**
+ * Create or Update the parent {{ parentTable.phpName }} object
+ * And return its primary key
+ *
+ * @return {{ parentTable.primaryKey[0].phpType }} The primary key of the parent object
+ */
+ public function getSyncParent($con = null)
+ {
+ $parent = $this->getParentOrCreate($con);
+ {% for column in parentTable.columns if not column.primaryKey and column.name != behavior.parameter('descendant_column') %}
+ $parent->set{{ column.phpName }}($this->get{{ column.phpName }}());
+ {% endfor %}
+
+ {% for foreignKey in parentTable.foreignKeys if (foreignKey.isParentChild is not defined) or (foreignKey.isParentChild is defined and foreignKey.isParentChild) %}
+ {# TODO: this var should be a class member and not added on runtime #}
+ {% set refPhpName = builder.fKPhpNameAffix(foreignKey, false) %}
+ if ($this->get{{ refPhpName }}() && $this->get{{ refPhpName }}()->isNew()) {
+ $parent->set{{ refPhpName }}($this->get{{ refPhpName }}());
+ }
+ {% endfor %}
+
+ return $parent;
+ }
+{% endif %}
View
25 src/Propel/Generator/Behavior/ConcreteInheritance/templates_parent/Object/_methods.php.twig
@@ -0,0 +1,25 @@
+/**
+ * Whether or not this object is the parent of a child object
+ *
+ * @return bool
+ */
+public function hasChildObject()
+{
+ return $this->{{ behavior.columnGetter }}() !== null;
+}
+
+/**
+ * Get the child object of this object
+ *
+ * @return mixed
+ */
+public function getChildObject()
+{
+ if (!$this->hasChildObject()) {
+ return null;
+ }
+ $childObjectClass = $this->{{ behavior.columnGetter }}();
+ $childObject = PropelQuery::from($childObjectClass)->findPk($this->getPrimaryKey());
+
+ return $childObject->hasChildObject() ? $childObject->getChildObject() : $childObject;
+}
View
57 src/Propel/Generator/Behavior/Delegate/DelegateBehavior.php
@@ -10,6 +10,7 @@
namespace Propel\Generator\Behavior\Delegate;
+use Propel\Generator\Builder\Om\AbstractOMBuilder;
use Propel\Generator\Model\Behavior;
use Propel\Generator\Model\ForeignKey;
@@ -104,36 +105,40 @@ protected function getDelegateTable($delegateTableName)
return $this->getTable()->getDatabase()->getTable($delegateTableName);
}
- public function objectCall($builder)
+ public function getDelegates()
{
- $plural = false;
- $script = '';
- foreach ($this->delegates as $delegate => $type) {
- $delegateTable = $this->getDelegateTable($delegate);
- if ($type == self::ONE_TO_ONE) {
- $fks = $delegateTable->getForeignKeysReferencingTable($this->getTable()->getName());
- $fk = $fks[0];
- $ARClassName = $builder->getClassNameFromBuilder($builder->getNewStubObjectBuilder($fk->getTable()));
- $ARFQCN = $builder->getNewStubObjectBuilder($fk->getTable())->getFullyQualifiedClassName();
- $relationName = $builder->getRefFKPhpNameAffix($fk, $plural);
- } else {
- $fks = $this->getTable()->getForeignKeysReferencingTable($delegate);
- $fk = $fks[0];
- $ARClassName = $builder->getClassNameFromBuilder($builder->getNewStubObjectBuilder($delegateTable));
- $ARFQCN = $builder->getNewStubObjectBuilder($delegateTable)->getFullyQualifiedClassName();
- $relationName = $builder->getFKPhpNameAffix($fk);
- }
- $script .= "
-if (is_callable(array('$ARFQCN', \$name))) {
- if (!\$delegate = \$this->get$relationName()) {
- \$delegate = new $ARClassName();
- \$this->set$relationName(\$delegate);
+ return $this->delegates;
}
- return call_user_func_array(array(\$delegate, \$name), \$params);
-}";
+ public function isOneToOne($type)
+ {
+ return $type === static::ONE_TO_ONE;
+ }
+
+ public function getDelegateForeignKey($type, $delegate)
+ {
+ if($type === static::ONE_TO_ONE) {
+ return $this->getDelegateTable($delegate)->getForeignKeysReferencingTable($this->getTable()->getName())[0];
+ } else {
+ return $this->getTable()->getForeignKeysReferencingTable($delegate)[0];
}
+ }
- return $script;
+ public function getDelegateObjectBuilder(AbstractOMBuilder $builder, $type, $delegate)
+ {
+ if($type === static::ONE_TO_ONE) {
+ return $builder->getNewStubObjectBuilder($this->getDelegateForeignKey($type, $delegate)->getTable());
+ } else {
+ return $builder->getNewStubObjectBuilder($this->getDelegateTable($delegate));
+ }
+ }
+
+ public function getDelegateMethodName(AbstractOMBuilder $builder, $type, $delegate)
+ {
+ if($type === static::ONE_TO_ONE) {
+ return $builder->getRefFKPhpNameAffix($this->getDelegateForeignKey($type, $delegate));
+ } else {
+ return $builder->getFKPhpNameAffix($this->getDelegateForeignKey($type, $delegate));
+ }
}
}
View
10 src/Propel/Generator/Behavior/Delegate/templates/Object/_hooks/_magicCall.php.twig
@@ -0,0 +1,10 @@
+{% for delegate, type in behavior.delegates %}
+ if (is_callable(array('{{ behavior.delegateObjectBuilder(builder, type, delegate).fullyQualifiedClassName }}', $name))) {
+ if (!$delegate = $this->get{{ behavior.delegateMethodName(builder, type, delegate) }}()) {
+ $delegate = new {{ builder.classNameFromBuilder(behavior.delegateObjectBuilder(builder, type, delegate)) }}();
+ $this->set{{ behavior.delegateMethodName(builder, type, delegate) }}($delegate);
+ }
+
+ return call_user_func_array(array($delegate, $name), $params);
+ }
+{% endfor %}

0 comments on commit 84310ba

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