/** * @param Node $parentNode * @param Node $prevNode * @return $this */ public function copyTo($parentNode, $prevNode = null) { $this->_tree->copyNodeTo($this, $parentNode, $prevNode); return $this; }
public function testTreeOperations() { $newNode1 = new Tree\Node('abc', 'node1', $this->_tree); $this->_tree->addNode($newNode1); $newNode2 = new Tree\Node('def', 'node2', $this->_tree); $this->_tree->addNode($newNode2, $newNode1); $newNode3 = new Tree\Node('ghi', 'node3', $this->_tree); $this->_tree->addNode($newNode3, $newNode1); $data1 = ['j', 'k', 'l']; $this->_tree->appendChild($data1, $newNode3); $newNode4 = new Tree\Node('mno', 'node4', $this->_tree); $this->_tree->appendChild($newNode4, $newNode3); $this->_tree->removeNode($newNode4); $this->_tree->removeNode($newNode3); $this->_tree->removeNode($newNode2); $this->_tree->removeNode($newNode1); $this->assertEmpty($this->_tree->getNodes()->getNodes()); }
/** * Db tree constructor * * $fields = array( * \Magento\Framework\Data\Tree\Dbp::ID_FIELD => string, * \Magento\Framework\Data\Tree\Dbp::PATH_FIELD => string, * \Magento\Framework\Data\Tree\Dbp::ORDER_FIELD => string * \Magento\Framework\Data\Tree\Dbp::LEVEL_FIELD => string * ) * * @param \Zend_Db_Adapter_Abstract $connection * @param string $table * @param array $fields * @throws \Exception */ public function __construct($connection, $table, $fields) { parent::__construct(); if (!$connection) { throw new \Exception('Wrong "$connection" parametr'); } $this->_conn = $connection; $this->_table = $table; if (!isset($fields[self::ID_FIELD]) || !isset($fields[self::PATH_FIELD]) || !isset($fields[self::LEVEL_FIELD]) || !isset($fields[self::ORDER_FIELD])) { throw new \Exception('"$fields" tree configuratin array'); } $this->_idField = $fields[self::ID_FIELD]; $this->_pathField = $fields[self::PATH_FIELD]; $this->_orderField = $fields[self::ORDER_FIELD]; $this->_levelField = $fields[self::LEVEL_FIELD]; $this->_select = $this->_conn->select(); $this->_select->from($this->_table); }
/** * @param Node $node * @return $this * @throws \Exception */ public function removeNode($node) { // For reorder old node branch $dataReorderOld = [$this->_orderField => new \Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) . '-1')]; $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField) . '=' . $node->getData($this->_parentField) . ' AND ' . $this->_conn->quoteIdentifier($this->_orderField) . '>' . $node->getData($this->_orderField); $this->_conn->beginTransaction(); try { $condition = $this->_conn->quoteInto("{$this->_idField}=?", $node->getId()); $this->_conn->delete($this->_table, $condition); // Update old node branch $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld); $this->_conn->commit(); } catch (\Exception $e) { $this->_conn->rollBack(); throw new \Exception('Can\'t remove tree node'); } parent::removeNode($node); return $this; }