Skip to content

Commit

Permalink
DelegateBehavior: toArray() now returns properties of delegated tables
Browse files Browse the repository at this point in the history
  • Loading branch information
SCIF committed Apr 13, 2015
1 parent e6e18a2 commit b411c0a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Propel/Generator/Behavior/Delegate/DelegateBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Propel\Generator\Model\Behavior;
use Propel\Generator\Model\ForeignKey;
use Propel\Generator\Util\PhpParser;

/**
* Gives a model class the ability to delegate methods to a relationship.
Expand Down Expand Up @@ -136,4 +137,36 @@ public function objectCall($builder)

return $script;
}

public function objectFilter(&$script)
{
$p = new PhpParser($script, true);
$text = $p->findMethod('toArray', true);
$matches = [];
preg_match('/(\$result = array\(([^;]+)\);)/U', $text, $matches);
$values = rtrim($matches[2]) . "\n";
$new_result = '';
$indent = ' ';

foreach ($this->delegates as $key => $value) {
$delegateTable = $this->getDelegateTable($key);

$ns = $delegateTable->getNamespace() ? '\\'.$delegateTable->getNamespace() : '';
$new_result .= "\$keys_{$key} = {$ns}\\Map\\{$delegateTable->getPhpName()}TableMap::getFieldNames(\$keyType);\n";
$i = 0;
foreach ($delegateTable->getColumns() as $column) {
if (!$column->isAutoIncrement()) {
$values .= "{$indent} \$keys_{$key}[{$i}] => \$this->get{$column->getPhpName()}(),\n";
}
$i++;
}
}
$new_result .= "{$indent}\$result = array({$values}\n{$indent});";
$text = str_replace($matches[1], $new_result , $text);
$p->replaceMethod('toArray', $text);
$script = $p->getCode();

return $script;
}

}

0 comments on commit b411c0a

Please sign in to comment.