Ejemplo n.º 1
0
 /**
  * Add an element into another as its child.
  *
  * @param $model|App_NestedSet_Model    Nested set model
  * @param $name|string              Name of the element
  * @param $reference|int            Id of the reference element
  *
  * @return $this
  */
 public function addInto(App_NestedSet_Model $nestedset, $name, $reference)
 {
     $db = $nestedset->getDb();
     // get parent's right value
     $select = $db->select();
     $select->from($nestedset->getTableName(), $nestedset->getStructureRight());
     $select->where("{$nestedset->getStructureId()} = {$reference}");
     $stmt = $db->query($select);
     $result = $stmt->fetch();
     $right = $result[$nestedset->getStructureRight()];
     try {
         $db->beginTransaction();
         // move next elements' right to make room
         $stmt = $db->query("\n                UPDATE {$nestedset->getTableName()}\n                   SET {$nestedset->getStructureRight()} = {$nestedset->getStructureRight()} + 2\n                 WHERE {$nestedset->getStructureRight()} > {$right};\n            ");
         $update = $stmt->fetch();
         // move next elements' left
         $stmt = $db->query("\n                UPDATE {$nestedset->getTableName()}\n                   SET {$nestedset->getStructureLeft()} = {$nestedset->getStructureLeft()} + 2\n                 WHERE {$nestedset->getStructureLeft()} > {$right};\n            ");
         $update = $stmt->fetch();
         // make room into parent element
         $stmt = $db->query("\n                UPDATE {$nestedset->getTableName()}\n                   SET {$nestedset->getStructureRight()} = {$nestedset->getStructureRight()} + 2\n                 WHERE {$nestedset->getStructureId()} = :reference;\n            ", array('reference' => $reference));
         $update = $stmt->fetch();
         // insert new element
         $values = array($nestedset->getStructureName() => $name, $nestedset->getStructureLeft() => $right, $nestedset->getStructureRight() => $right + 1);
         $db->insert($nestedset->getTableName(), $values);
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Get the parent of an element.
  *
  * @param $model|App_NestedSet_Model    Nested set model
  * @param $elementId|int    Element ID
  * @param $depth|int        Depth of the parent, compared to the child.
  *                          Default is 1 (as immediate)
  *
  * @return array|false
  */
 public function getParent(App_NestedSet_Model $nestedset, $elementId, $depth = 1)
 {
     $db = $nestedset->getDb();
     $select = $db->select()->from($nestedset->getTableName(), array($nestedset->getStructureLeft(), $nestedset->getStructureRight()))->where($nestedset->getStructureId() . ' = ?', $elementId);
     $stmt = $db->query($select);
     $child = $stmt->fetch();
     $select = $db->select()->from($nestedset->getTableName(), array($nestedset->getStructureId(), $nestedset->getStructureName()))->where($nestedset->getStructureLeft() . ' < ?', $child[$nestedset->getStructureLeft()])->where($nestedset->getStructureRight() . ' > ?', $child[$nestedset->getStructureRight()])->order('(' . $child[$nestedset->getStructureLeft()] . ' - ' . $nestedset->getStructureLeft() . ')')->limitPage($depth, 1);
     $stmt = $db->query($select);
     $result = $stmt->fetch();
     return $result;
 }