public function insertLeftOf(TreeModel &$siblingNode)
{
if ($siblingNode->lft >= $siblingNode->rgt) {
throw new TreeInvalidLftRgtSibling();
}
// Get a reference to the database
$db = $this->getDbo();
// Get the field names
$fldRgt = $db->qn($this->getFieldAlias('rgt'));
$fldLft = $db->qn($this->getFieldAlias('lft'));
// Nullify the PK, so a new record will be created
$this->{$this->idFieldName} = null;
// Get the value of the parent node's rgt
$myLeft = $siblingNode->lft;
// Update my lft/rgt values
$this->lft = $myLeft;
$this->rgt = $myLeft + 1;
// Update sibling's lft/rgt values
$siblingNode->lft += 2;
$siblingNode->rgt += 2;
$db->transactionStart();
try {
$db->setQuery($db->getQuery(true)->update($db->qn($this->tableName))->set($fldLft . ' = ' . $fldLft . '+2')->where($fldLft . ' >= ' . $db->q($myLeft)))->execute();
$db->setQuery($db->getQuery(true)->update($db->qn($this->tableName))->set($fldRgt . ' = ' . $fldRgt . '+2')->where($fldRgt . ' > ' . $db->q($myLeft)))->execute();
$this->save();
// Commit the transaction
$db->transactionCommit();
} catch (\Exception $e) {
$db->transactionRollback();
throw $e;
}
return $this;
}