One more thing how to use the delete function? If just get the model instance and delete it will not be called… and will not remove the descendants. Shouldnt it use the beforeDelete event?
Because if i change it too:
/**
* Deletes node and it's descendants.
* @return boolean whether the deletion is successful.
*/
public function beforeDelete($event) {
parent::beforeDelete($event);
$owner=$this->getOwner();
if($owner->getIsNewRecord())
throw new CDbException(Yii::t('yiiext','The node cannot be deleted because it is new.'));
$transaction=$owner->getDbConnection()->beginTransaction();
try
{
$root=$this->hasManyRoots ? $owner->{$this->root} : null;
if($owner->isLeaf())
$result=$owner->delete();
else
{
$condition=$this->left.'>='.$owner->{$this->left}.' AND '.
$this->right.'<='.$owner->{$this->right};
if($root!==null)
$condition.=' AND '.$this->root.'='.$root;
$result=$owner->deleteAll($condition)>0;
}
if($result)
{
$first=$owner->{$this->right}+1;
$delta=$owner->{$this->left}-$owner->{$this->right}-1;
$this->shiftLeftRight($first,$delta,$root);
$transaction->commit();
return true;
}
}
catch(Exception $e)
{
$transaction->rollBack();
}
return false;
}
Both with the commented attributes set and unset. I feel this implementation detail should be hidden, I didn’t even know if the counters start at zero or one until I read some comments in this thread, and maybe it is hidden and I don’t have to care about it. Just still didn’t find out.
In the end, this always throws an exception “Many roots mode is off”. And since it’s a new record and since I can’t save the AR using CActiveRecord::save, I’m feeling kind of lost here.
In the end, I created the root node directly in the DB (lft:1, rgt:2, level: 1, root: null) and managed to insert a node using the following snippet of code:
Is there a chance to get some small code snippets for each of the provided API methods? For example it seems to me as if the saveNode method is useless in single root mode, but maybe I’m using it the wrong way.
I’m trying to understand what values are stored in the ‘root’ field and the ‘level’ field. I wouldn’t need to know this except that I’m building a backoffice administration app that we’ll most likely use to configure our categories. I want to make sure I use the correct logic so that this extension will work if and when I want to use it.
First of all, Thank you for implementing nested tree!
I’m facing a problem atm. The situation:
I’m making a webshop with nested categories, using the nested tree. When someone deletes a category, the program seeks for products assigned to that category and its subcategories, if found, the category must be not deleted. So I run the event beforeDelete. This works like a charm when the category is a leaf, but when it has subcategories the event doesn’t get raised because of deleteAll() used in nestedtree’s delete() method.
I think I will manually raise the event in the controller before deleting (if the category is not a leaf) and lean on the event’s return. What do you think? Anyone had a similar case?
The thing is done, tho I had to modify the extension.
I had to implement a new method into it:
public function setIgnoreEvent($ignoreEvent)
{
$this->_ignoreEvent=$ignoreEvent;
}
I did this because in the behavior beforeDelete() method checks for _ignoreEvent and if it is false, the event raising stops. What do you think? I think you should include this into the class.
So the code looks like:
$toDelete = true;
$category->tree->setIgnoreEvent(true);
if (!$category->beforeDelete()) {
$toDelete = false;
}
$category->tree->setIgnoreEvent(false);
if ($toDelete && $category->tree->deleteNode()) {
$this->messages[] = 'Sikeresen letöröltem a kategóriát.';
} else {
$this->messages['hasError'] = 1;
$this->messages[] = 'Nem tudtam letörölni a kategóriát!';
foreach ($category->getErrors() as $field => $error) {
$this->messages[] = $error[0];
}
}
It is already in the (product) model. The event handler calls it:
public static function handleProductCategoryDelete(&$event)
{
$productCount = self::categoryHasProducts($event->sender);
if ((int)$productCount > 0) {
$event->message = "A kategória tartalmaz {$productCount} db terméket, ezeket le kell először törölni.";
}
}
Hey, thanks for the ext, it’s very usefull, especially with ENestedSetBehavior2 improvement, that is suggested in comments to nestedsetbehavior extension page.
But I’d like to ask you, if there is any chance that you will add the support for parent_id field? Or I’ll (and other people who we’ll need it) have to add it manualy?
Meanwhile, I solved this problem by extending model in this way:
<?php
class Communicate extends CActiveRecord
{
// Will be storing parent id here
public $parent_id;
/**
* Communicate with id of its parent.
* Id of parent will be stored in ->parent_id
*/
public function withParent()
{
$owner=$this->getOwner();
$db=$owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$criteria->select .= ', `parent`.ID as `parent_id`';
$select =
' SELECT * from ' . $db->quoteColumnName($owner->tableName()) .
' WHERE ' . $db->quoteColumnName($owner->tableName()) . '.ROOT = ' . $this->ROOT .
' ORDER BY ' . $db->quoteColumnName($owner->tableName()) . '.LFT DESC';
$criteria->join .=
'LEFT JOIN ('.$select.') `parent` ' .
'ON (`parent`.LFT < `t`.LFT AND `parent`.RGT > `t`.RGT)';
$criteria->group = '`t`.ID';
return $owner;
}
/**
* Need to populate extended data
*/
public function populateRecord($attributes,$callAfterFind=true)
{
$record = parent::populateRecord($attributes, $callAfterFind);
if ( $record )
{
//getting parent id if it's set
if ( isset($attributes['parent_id']) )
$record->parent_id = $attributes['parent_id'];
}
return $record;
}
}
// And I'm triggering this like:
Communicate::model()->withParent()->findAll();
Maybe this will help somebody, cause getting parent ids of more than one item is not trivial.
Possibly, it’s not the best solution, but works for me.
Anyway, thanks a lot for this extension, saved a lot of time.