Permalink
Browse files

sortable behavior: changed moveRelatedObjectsToNullScope() method to …

…use improved shiftRank() method
  • Loading branch information...
1 parent 4c3cf04 commit 1e16f2cdb0edb2bba2fd885fce1bfd5990bbe25f @rozwell rozwell committed Aug 25, 2012
@@ -38,22 +38,23 @@ class SortableBehavior extends Behavior
*/
public function modifyTable()
{
- if (!$this->getTable()->containsColumn($this->getParameter('rank_column'))) {
- $this->getTable()->addColumn(array(
+ $table = $this->getTable();
+
+ if (!$table->containsColumn($this->getParameter('rank_column'))) {
+ $table->addColumn(array(
'name' => $this->getParameter('rank_column'),
'type' => 'INTEGER'
));
}
if ($this->useScope() &&
- !$this->getTable()->containsColumn($this->getParameter('scope_column'))) {
- $this->getTable()->addColumn(array(
+ !$table->containsColumn($this->getParameter('scope_column'))) {
+ $table->addColumn(array(
'name' => $this->getParameter('scope_column'),
'type' => 'INTEGER'
));
}
if ($this->useScope()) {
- $table = $this->getTable();
$keys = $table->getColumnForeignKeys($this->getParameter('scope_column'));
foreach ($keys as $key) {
if ($key->isForeignPrimaryKey() && $key->getOnDelete() == ForeignKey::SETNULL) {
@@ -69,34 +69,30 @@ protected function addObjectMoveRelatedToNullScope(&$script)
{
$database = $this->getTable()->getDatabase();
$tableName = $database->getTablePrefix() . $this->getParameter('foreign_table');
+ $peer = $this->builder->getNewStubPeerBuilder($this->getForeignTable())->getClassname();
$maxSql = sprintf('SELECT MAX(%s) FROM %s WHERE %s IS NULL',
$this->getForeignColumnForParameter('foreign_rank_column')->getName(),
$database->getPlatform()->quoteIdentifier($tableName),
$this->getForeignColumnForParameter('foreign_scope_column')->getFullyQualifiedName()
);
- $updateSql = sprintf('UPDATE %s SET %s = %s+:p1 WHERE %s = :p2',
- $database->getPlatform()->quoteIdentifier($tableName),
- $this->getForeignColumnForParameter('foreign_rank_column')->getName(),
- $this->getForeignColumnForParameter('foreign_rank_column')->getName(),
- $this->getForeignColumnForParameter('foreign_scope_column')->getFullyQualifiedName()
- );
$script .= "
/**
* Moves related {$this->getRelatedClassPluralForm()} to null scope
* @param PropelPDO \$con A connection object
*/
-public function {$this->getObjectMoveRelatedToNullScopeMethodName()}(PropelPDO \$con)
+public function {$this->getObjectMoveRelatedToNullScopeMethodName()}(PropelPDO \$con = null)
{
+ if (\$con === null) {
+ \$con = Propel::getConnection($peer::DATABASE_NAME, Propel::CONNECTION_WRITE);
+ }
+
\$stmt = \$con->prepare('$maxSql');
\$stmt->execute();
\$maxRank = (int)\$stmt->fetchColumn();
- \$stmt = \$con->prepare('$updateSql');
- \$stmt->bindValue(':p1', \$maxRank);
- \$stmt->bindValue(':p2', \$this->getPrimaryKey());
- \$stmt->execute();
+ $peer::shiftRank(\$maxRank, null, null, \$this->getPrimaryKey(), \$con);
}
";

0 comments on commit 1e16f2c

Please sign in to comment.