Skip to content

Commit

Permalink
Add basic Readme and a minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev committed Oct 19, 2011
1 parent 7dbbe6d commit 34a877c
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 167 deletions.
92 changes: 46 additions & 46 deletions EqualNestBehavior.php
Expand Up @@ -4,9 +4,9 @@

/**
* Gives a model class the ability to support Equal Nest relations
*
*
* @author Ivan Plamenov Tanev aka Crafty_Shadow @ WEBWORLD.BG <vankata.t@gmail.com>
*
* @author Ivan Plamenov Tanev aka Crafty_Shadow @ WEBWORLD.BG <vankata.t@gmail.com>
* ?package propel.generator.behavior.equal_nest
*/
class EqualNestBehavior extends Behavior
Expand All @@ -16,11 +16,11 @@ class EqualNestBehavior extends Behavior
'parent_table' => null,
'reference_column_1' => null,
'reference_column_2' => null,
);
);

/** @var EqualNestParentBehavior */
protected $parentBehavior = null;

public function modifyTable()
{
$table = $this->getTable();
Expand All @@ -30,7 +30,7 @@ public function modifyTable()
throw new Exception('Equal nest works only with a single primary key for the parent table');
}
$parentTablePrimaryKey = $parentTable->getPrimaryKey();

if (!$this->getTable()->containsColumn($this->getRefecenceColumn1Name()))
{
$this->getTable()->addColumn(array(
Expand All @@ -48,7 +48,7 @@ public function modifyTable()
$this->getTable()->addForeignKey($fk);
}


if (!$this->getTable()->containsColumn($this->getRefecenceColumn2Name()))
{
$this->getTable()->addColumn(array(
Expand All @@ -64,93 +64,93 @@ public function modifyTable()
$fk->addReference($this->getRefecenceColumn2Name(), $parentTablePrimaryKey[0]->getName());
$this->getTable()->addForeignKey($fk);
}


if (!$parentTable->hasBehavior('equal_nest_parent'))
{
$parentBehavior = new EqualNestParentBehavior();
$parentBehavior->setName('equal_nest_parent');
$parentBehavior->addParameter(array('name' => 'middle_table', 'value' => $this->getTable()->getName()));
$parentTable->addBehavior($parentBehavior);
}

$this->parentBehavior = $parentTable->getBehavior('equal_nest_parent');
}

public function staticMethods($builder)
{
$this->builder = $builder;
$script = '';
$this->addPeerBuildEqualNestRelation($script);
$this->addPeerRemoveEqualNestRelation($script);
$this->addPeerCheckForEqualNestRelation($script);

return $script;
}

public function addPeerBuildEqualNestRelation(&$script)
{
$script .= "
/**
* Builds a new equal nest relation between {$this->parentBehavior->getTable()->getPhpName()} objects
*
*
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object1
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object2
* @param PropelPDO \$con
* @return void
*/
*/
public static function buildEqualNest{$this->getTable()->getPhpName()}Relation(\$object1, \$object2, PropelPDO \$con = null)
{
if (self::checkForExistingEqualNest{$this->getTable()->getPhpName()}Relation(\$object1, \$object2, \$con = null))
{
throw new PropelException('[Equal Nest] The relation between those two objects aready exists');
}
return;
}
\$a{$this->getTable()->getPhpName()} = new {$this->getTable()->getPhpName()}();
\$a{$this->getTable()->getPhpName()}->set{$this->getTable()->getColumn($this->getRefecenceColumn1Name())->getPhpName()}(is_object(\$object1) ? \$object1->getPrimaryKey() : \$object1);
\$a{$this->getTable()->getPhpName()}->set{$this->getTable()->getColumn($this->getRefecenceColumn2Name())->getPhpName()}(is_object(\$object2) ? \$object2->getPrimaryKey() : \$object2);
\$a{$this->getTable()->getPhpName()}->save();
}
";
}

public function addPeerRemoveEqualNestRelation(&$script)
{
$script .= "
/**
* Removes a new equal nest relation between {$this->parentBehavior->getTable()->getPhpName()} objects
*
*
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object1
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object2
* @param PropelPDO \$con
* @return void
*/
*/
public static function removeEqualNest{$this->getTable()->getPhpName()}Relation(\$object1, \$object2, PropelPDO \$con = null)
{
if (!\$relation = self::checkForExistingEqualNest{$this->getTable()->getPhpName()}Relation(\$object1, \$object2, \$con = null))
{
throw new PropelException('[Equal Nest] Cannot remove a relation that does not exist.');
}
}
\$relation->delete();
}
";
";
}

public function addPeerCheckForEqualNestRelation(&$script)
{
$fullNameRefColumn1 = $this->table->getPhpName(). '.' .$this->getReferenceColumn1()->getPhpName();
$fullNameRefColumn2 = $this->table->getPhpName(). '.' .$this->getReferenceColumn2()->getPhpName();

$script .= "
/**
* Checks whether an equal nest relation between {$this->parentBehavior->getTable()->getPhpName()} objects
*
*
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object1
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object2
* @param PropelPDO \$con
* @return {$this->getTable()->getPhpName()}|false
*/
*/
public static function checkForExistingEqualNest{$this->getTable()->getPhpName()}Relation(\$object1, \$object2, PropelPDO \$con = null)
{
if (\$object1 instanceof {$this->parentBehavior->getTable()->getPhpName()} && \$object1->isNew()) return false;
Expand All @@ -162,21 +162,21 @@ public static function checkForExistingEqualNest{$this->getTable()->getPhpName()
}
";
}


public function queryMethods($builder)
{
$this->builder = $builder;
$fullNameRefColumn1 = $this->table->getPhpName(). '.' .$this->getReferenceColumn1()->getPhpName();
$fullNameRefColumn2 = $this->table->getPhpName(). '.' .$this->getReferenceColumn2()->getPhpName();
$script = '';
$fullNameRefColumn2 = $this->table->getPhpName(). '.' .$this->getReferenceColumn2()->getPhpName();

$script = '';

// filter by relation
$script .= "
/**
* Filter the query by 2 {$this->parentBehavior->getTable()->getPhpName()} objects for a Equal Nest {$this->getTable()->getPhpName()} relation
*
*
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object1
* @param {$this->parentBehavior->getTable()->getPhpName()}|integer \$object2
* @return {$this->builder->getStubQueryBuilder()->getClassname()}
Expand All @@ -191,23 +191,23 @@ public function filterBy{$this->builder->getPluralizer()->getPluralForm($this->p
->combine(array('first-one', 'first-two'), 'AND', 'first')
->combine(array('second-one', 'second-two'), 'AND', 'second')
->where(array('first', 'second'), 'OR');
}
}
";

return $script;
}

protected function getParentTable()
{
if (null === $this->getParameter('parent_table'))
{
throw new Exception('You must set a parent table for the Equal Nest behavior');
throw new Exception('You must set a parent table for the Equal Nest behavior');
}

return $this->getTable()->getDatabase()->getTable($this->getParameter('parent_table'));
}
}


public function getRefecenceColumn1Name()
{
if (null === $this->getParameter('reference_column_1'))
Expand All @@ -218,7 +218,7 @@ public function getRefecenceColumn1Name()
{
return $this->getParameter('reference_column_1');
}
}
}

public function getRefecenceColumn2Name()
{
Expand All @@ -230,18 +230,18 @@ public function getRefecenceColumn2Name()
{
return $this->getParameter('reference_column_2');
}
}
}

public function getReferenceColumn1()
{
return $this->table->getColumn($this->getRefecenceColumn1Name());
}

public function getReferenceColumn2()
{
return $this->table->getColumn($this->getRefecenceColumn2Name());
}


}

0 comments on commit 34a877c

Please sign in to comment.