/** * This function removes a node and its descendants. * * @access public * @override * @param boolean $reset whether to reset each column's value back * to its original value * @throws Throwable_Marshalling_Exception indicates that the record could not be * deleted * * @see http://imrannazar.com/Modified-Preorder-Tree-Traversal * @see http://stackoverflow.com/questions/1473459/getting-modified-preorder-tree-traversal-data-into-an-array */ public function delete($reset = FALSE) { if (!static::is_savable()) { throw new Throwable_Marshalling_Exception('Message: Failed to delete record from database. Reason: Model is not savable.', array(':class' => get_called_class())); } $data_source = static::data_source(DB_DataSource::MASTER_INSTANCE); $table = static::table(); $connection = DB_Connection_Pool::instance()->get_connection($data_source); $connection->lock->add($table)->acquire(); $select = DB_SQL::select($data_source)->column('t1.parent_id')->from($table, 't1')->where('t1.scope', DB_SQL_Operator::_EQUAL_TO_, $this->fields['scope']->value)->where('t1.id', DB_SQL_Operator::_NOT_EQUIVALENT_, $this->fields['id']->value)->where('t1.lft', DB_SQL_Operator::_LESS_THAN_, DB_SQL::expr('t0.lft'))->where('t1.rgt', DB_SQL_Operator::_GREATER_THAN_, DB_SQL::expr('t0.rgt'))->order_by(DB_SQL::expr('t1.rgt - t0.rgt'))->limit(1); $update = DB_SQL::update($data_source)->set('t0.parent_id', $select)->set('t0.lft', DB_ORM::expr('t0.lft - 2'))->set('t0.rgt', DB_ORM::expr('t0.rgt - 2'))->table($table, 't0')->where('t0.scope', DB_SQL_Operator::_EQUAL_TO_, $this->fields['scope']->value)->where('t0.lft', DB_SQL_Operator::_GREATER_THAN_, $this->fields['rgt']->value)->statement(); $connection->execute($update); $delete = DB_SQL::delete($data_source)->from($table)->where('scope', DB_SQL_Operator::_EQUAL_TO_, $this->fields['scope']->value)->where('id', DB_SQL_Operator::_EQUAL_TO_, $this->fields['id']->value)->statement(); $connection->execute($delete); $connection->lock->release(); if ($reset) { $this->reset(); } else { $this->metadata['saved'] = NULL; } }
/** * This function deletes the record matching the primary key from the database. * * @access public * @param boolean $reset whether to reset each column's value back * to its original value * @throws Throwable_Marshalling_Exception indicates that the record could not be * deleted */ public function delete($reset = FALSE) { if (!static::is_savable()) { throw new Throwable_Marshalling_Exception('Message: Failed to delete record from database. Reason: Model is not savable.', array(':class' => get_called_class())); } $primary_key = static::primary_key(); if (empty($primary_key) or !is_array($primary_key)) { throw new Throwable_Marshalling_Exception('Message: Failed to delete record from database. Reason: No primary key has been declared.'); } $builder = DB_SQL::delete(static::data_source(DB_DataSource::MASTER_INSTANCE))->from(static::table()); foreach ($primary_key as $column) { $builder->where($column, DB_SQL_Operator::_EQUAL_TO_, $this->fields[$column]->value); } $builder->execute(); if ($reset) { $this->reset(); } else { $this->metadata['saved'] = NULL; } }