Sorry i’ve been so quiet, last half year has been quiet busy and since I’m currently not actively developing using Yii (I’m planning to start again next month) this extension didn’t have a high priority for me.
I’ve just added a new release which fixes most bugs that were posted here and in the reviews section. Please see the changelog for more information.
I’m currently in the final 3 weeks of my study and very busy with moving and my ‘new’ job, so please be patient. I will add the new proposed features (like scopes) as soon as I have more time. Sadly, I don’t have time to dive into Yii 1.1 at the moment, so if some could help me making it compatible it would be greatly appreciated.
It works nice with Yii 1.1rc, you just have to add ->Owner or ->getOwner() to $this->getIsNewRecord(), $sibling->getIsNewRecord(), $node->getIsNewRecord() and $node->save().
I think it’s also good to change $node->save() to $node->Owner->save(false) or if(!$node->Owner->save()) throw new TreeException(print_r($node->Owner->getErrors(), true)).
It may need to add methods: getParents, getLeaves and etc. ?
/**
* Returns the parents of this node
* @param boolean $root
* @return array
*/
public function getParents($root = true) {
$builder = $this->Owner->getCommandBuilder();
$cstring = $this->_lftCol." < ? AND ".$this->_rgtCol." > ?";
if ( ! $root)
$cstring .= " AND ".$this->_lftCol ." !=1";
$cstring .= " ORDER BY ".$this->_lftCol. " ASC";
$criteria = $builder->createCriteria($cstring,array($this->getLeftValue(), $this->getRightValue()));
$command = $builder->createFindCommand($this->Owner->getTableSchema(),$criteria);
return $this->Owner->populateRecords($command->queryAll());
}
/**
* Returns a list of leaf nodes.
* @return array
*/
public function getLeaves() {
$builder = $this->Owner->getCommandBuilder();
$cstring = $this->_lftCol . " = (".$this->_rgtCol." - 1) AND "
.$this->_lftCol." >= ? AND "
.$this->_rgtCol." <= ? ORDER BY ".$this->_lftCol. " ASC";
$criteria = $builder->createCriteria($cstring,array($this->getLeftValue(),$this->getRightValue()));
$command = $builder->createFindCommand($this->Owner->getTableSchema(),$criteria);
return $this->Owner->populateRecords($command->queryAll());
}
/**
* Is the current node a root node?
* @return boolean
*/
public function isRoot() {
return ($this->getLeftValue() === 1);
}
/**
* Is the current node a leaf node
* @return boolean
*/
public function isLeaf() {
return !$this->hasChildNodes();
}
/**
* Is the current node a direct child of the supplied node
* @param object node object
* @return boolean
*/
public function isChild($target) {
return ($this->getParentNode()->getIdValue() === $target->getIdValue());
}
/**
* Is the current node the direct parent of the supplied node
*
* @param object node object
* @return boolean
**/
public function isParent($target) {
return ($this->getIdValue() === $target->getParentNode()->getIdValue());
}
/**
* Is the current node a sibling of the supplied node
*
* @param object node object
* @return boolean
**/
public function isSibling($target) {
return ($this->getParentNode()->getIdValue() === $target->getParentNode()->getIdValue());
}
/**
* Get Size
*
* Returns the current size of the node.
*
* @return int
**/
protected function getSize() {
return ($this->getRightValue() - $this->getLeftValue()) + 1;
}
I tried to move a node with appendChild method as it supposed. In the beginning i got this error:
I opened the TreeBehavior.phpAnd function was
public function appendChild($node, $brother = null)
{
// Fetch nodes information
$parent = $this;
$transaction= $this->Owner->dbConnection->beginTransaction();
if($this->Owner->getIsNewRecord())
throw new TreeException('You can\'t append a node to a parent that is not yet in the database. Add the parent node to the tree first using AppendChild or using an Insert* method.');
if(!$node->getIsNewRecord())
{
return $node->moveBelow($this);
}
try
{.....
Transaction begin before moveBelow so i move it after if like:
public function appendChild($node, $brother = null)
{
// Fetch nodes information
$parent = $this;
if($this->Owner->getIsNewRecord())
throw new TreeException('You can\'t append a node to a parent that is not yet in the database. Add the parent node to the tree first using AppendChild or using an Insert* method.');
if(!$node->getIsNewRecord())
{
return $node->moveBelow($this);
}
$transaction= $this->Owner->dbConnection->beginTransaction();
try
{.....
2010/05/16 22:43:03 [error] [application.extensions.nestedset.treebehavior] Error appending node, transaction aborted. Exception: Class TreeBehavior does not have method with name "save".
in /home/dmtrsslvdr/public_html/wwwTrans/protected/extensions/nestedset/TreeBehavior.php (435)
in /home/dmtrsslvdr/public_html/wwwTrans/protected/extensions/nestedset/TreeBehavior.php (553)
in /home/dmtrsslvdr/public_html/wwwTrans/protected/controllers/TreeController.php (359)