Last active
March 26, 2018 09:48
-
-
Save pyguerder/30701e6e1df6949122989a4f20b82789 to your computer and use it in GitHub Desktop.
Propel2 setEditor behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Core\CoreBundle\Propel; | |
use Propel\Generator\Model\Behavior; | |
use Propel\Generator\Model\ForeignKey; | |
/** | |
* Adds a setEditor method to the model classes. | |
* | |
* @author Pierre-Yves Guerder | |
*/ | |
class SetEditorBehavior extends Behavior | |
{ | |
protected $parameters = [ | |
'foreign_table' => null, | |
'foreignColumn' => null, | |
'created_by_php_name' => null, | |
'created_by_ref_php_name' => null, | |
'updated_by_php_name' => null, | |
'updated_by_ref_php_name' => null, | |
]; | |
public function modifyTable() | |
{ | |
$table = $this->getTable(); | |
$database = $table->getDatabase(); | |
$foreignTable = $database->getTable($this->getParameter('foreign_table')); | |
$foreignColumn = $foreignTable->getColumn($this->getParameter('foreign_column')); | |
$createdByPhpName = $this->getParameter('created_by_php_name'); | |
$createdByRefPhpName = $this->getParameter('created_by_ref_php_name'); | |
$updatedByPhpName = $this->getParameter('updated_by_php_name'); | |
$updatedByRefPhpName = $this->getParameter('updated_by_ref_php_name'); | |
if (!$table->hasColumn('created_by_id')) { | |
$column = $table->addColumn(['name' => 'created_by_id', 'type' => 'INTEGER']); | |
$fk = new ForeignKey(); | |
$fk->setForeignTableCommonName($foreignTable->getCommonName()); | |
$fk->setForeignSchemaName($foreignTable->getSchema()); | |
if ($createdByPhpName) { | |
$fk->setPhpName($createdByPhpName); | |
} | |
if ($createdByRefPhpName) { | |
$fk->setRefPhpName($createdByRefPhpName); | |
} | |
$fk->setDefaultJoin('LEFT JOIN'); | |
// $fk->setOnDelete(ForeignKey::RESTRICT); // implicit | |
$fk->setOnUpdate(ForeignKey::NONE); | |
$fk->addReference($column->getName(), $foreignColumn->getName()); | |
$table->addForeignKey($fk); | |
} | |
if (!$table->hasColumn('updated_by_id')) { | |
$column = $table->addColumn(['name' => 'updated_by_id', 'type' => 'INTEGER']); | |
$fk = new ForeignKey(); | |
$fk->setForeignTableCommonName($foreignTable->getCommonName()); | |
$fk->setForeignSchemaName($foreignTable->getSchema()); | |
if ($updatedByPhpName) { | |
$fk->setPhpName($updatedByPhpName); | |
} | |
if ($updatedByRefPhpName) { | |
$fk->setRefPhpName($updatedByRefPhpName); | |
} | |
$fk->setDefaultJoin('LEFT JOIN'); | |
// $fk->setOnDelete(ForeignKey::RESTRICT); // implicit | |
$fk->setOnUpdate(ForeignKey::NONE); | |
$fk->addReference($column->getName(), $foreignColumn->getName()); | |
$table->addForeignKey($fk); | |
} | |
} | |
public function objectMethods() | |
{ | |
$script = $this->setEditor(); | |
return $script; | |
} | |
protected function setEditor() | |
{ | |
$table = $this->getTable(); | |
$database = $table->getDatabase(); | |
$foreignTable = $database->getTable($this->getParameter('foreign_table')); | |
$foreignModelName = $foreignTable->getPhpName(); | |
$foreignColumn = $foreignTable->getColumn($this->getParameter('foreign_column')); | |
$foreignColumnPhpName = $foreignColumn->getPhpName(); | |
return " | |
/** | |
* Set the value of [createdBy] and [updatedBy] columns. | |
* | |
* @param {$foreignModelName} \$user The user object to refer to. | |
* | |
* @return self The current object (for fluent API support) | |
*/ | |
public function setEditor({$foreignModelName} \$user) | |
{ | |
if (\$this->isNew()) { | |
\$this->setCreatedById(\$user->get{$foreignColumnPhpName}()); | |
} | |
\$this->setUpdatedById(\$user->get{$foreignColumnPhpName}()); | |
return \$this; | |
} | |
"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment