Yii Framework Forum: Nestedset扩展升级重建树功能 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Nestedset扩展升级重建树功能 Rate Topic: -----

#1 User is offline   sharehua 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 456
  • Joined: 30-December 08

Posted 30 April 2010 - 04:21 AM

http://www.yiiframew...tree-extension/
这个tree扩展比较全,但我用的是老早的nestedset,升级比较痛苦,就添加了qq71151461的两个方法过来。在这里感谢qq71151461 :rolleyes:


这两个方法是实现重建树的功能。如果你原库是用parentId来做树的,想要左右值来做树,则可以用这两个方法建树。
TreeBehavior这个类添加

public $_parentIdCol = "parentId";

	public function rebuildTree($parentId=0, $left=0) {
		$right = $left+1;
		$rows=$this->Owner->getDbConnection()->createCommand("SELECT `{$this->_idCol}` FROM `{$this->Owner->tableName()}` WHERE `{$this->_parentIdCol}`={$parentId}")->queryColumn();
		foreach ($rows as $id) {
			$right = $this->rebuildTree($id, $right);
		}
		$this->Owner->getDbConnection()->createCommand("UPDATE `{$this->Owner->tableName()}` SET `{$this->_lftCol}`={$left},`{$this->_rgtCol}`={$right} WHERE `{$this->_idCol}`=$parentId")->execute();
		return $right + 1;
	}
	public function rebuildTreeLevel() {
		$rows=$this->Owner->getDbConnection()->createCommand("SELECT `{$this->_idCol}`,`{$this->_lftCol}`,`{$this->_rgtCol}` FROM `{$this->Owner->tableName()}` ORDER BY `{$this->_lftCol}`")->queryAll();
		foreach ($rows as $row) {
			$level=$this->Owner->getDbConnection()->createCommand("SELECT COUNT(*) FROM `{$this->Owner->tableName()}` WHERE `{$this->_lftCol}`<={$row[$this->_lftCol]} AND `{$this->_rgtCol}`>{$row[$this->_rgtCol]}")->queryScalar();
			$this->Owner->getDbConnection()->createCommand("UPDATE `{$this->Owner->tableName()}` SET `{$this->_lvlCol}`={$level} WHERE `{$this->_idCol}`={$row[$this->_idCol]}")->execute();
		}
	}


调用:
Model::model()->rebuildTree();
Model::model()->rebuildTreeLevel();
YII2 技术群【QQ群--146409855】,欢迎朋友,拒绝过客
0

#2 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 30 April 2010 - 10:55 AM

不错~
可惜 nestedset作者都不更新扩展 ;(
0

#3 User is offline   qq71151461 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 386
  • Joined: 05-June 09

Posted 30 April 2010 - 06:20 PM

两个方法,注意先后
0

#4 User is offline   sharehua 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 456
  • Joined: 30-December 08

Posted 29 August 2010 - 03:25 AM

再增加一个 根据leve,lft,rgt更新 parentId的方法。

	public function rebuildTreeParent($parentId=1,$level=1, $left=0, $right) {
		$rows=$this->Owner->getDbConnection()->createCommand("SELECT `{$this->_idCol}`,`{$this->_lftCol}`,`{$this->_rgtCol}` FROM `{$this->Owner->tableName()}` WHERE `{$this->_lvlCol}`={$level} and `{$this->_lftCol}`>{$left} and `{$this->_rgtCol}`<{$right}")->queryAll();
		$this->Owner->getDbConnection()->createCommand("UPDATE `{$this->Owner->tableName()}` SET `{$this->_parentIdCol}`={$parentId} WHERE  `{$this->_lvlCol}`={$level} and `{$this->_lftCol}`>{$left} and `{$this->_rgtCol}`<{$right}")->execute();
		foreach ($rows as $row) {
			$this->rebuildTreeParent($row[$this->_idCol],$level+1, $row[$this->_lftCol], $row[$this->_rgtCol]);
		}
	}

YII2 技术群【QQ群--146409855】,欢迎朋友,拒绝过客
0

#5 User is offline   sharehua 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 456
  • Joined: 30-December 08

Posted 19 September 2010 - 11:16 PM

修复一个我认为的bug,说明:原版当 提取 level大于0的节点的数据的时候 $nestedTree = $root->getNestedTree();返回的本节点应该出现在node里面,但在原版出现在children,有点不合逻辑。

修改
public function getNestedTree($returnrootnode = true, $keyfield = null)
里面
$depth = $this->getLevelValue(); 原来是 $depth = 0;
YII2 技术群【QQ群--146409855】,欢迎朋友,拒绝过客
0

#6 User is offline   caridy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 14-March 11

Posted 01 July 2011 - 12:09 AM

我需要tree的帮助.
用了qq71151461的,添加子节点的时候提示lft等几个不能为空.
0

#7 User is offline   hiscaler 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 230
  • Joined: 17-October 09

Posted 02 July 2011 - 08:21 PM

rules 中要去掉 left_value right_value parnet_value level的required rule
0

#8 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 27-December 10
  • Location:china

Posted 07 November 2011 - 02:09 AM

重建功能不错的 试图做过从邻接表到嵌套集模型的相互切换 但最后还是有些问题 未能实现 这里说出这种可能 谁有兴趣可以试着实现下 ::)

留个足印 以后在回来 :lol:
0

#9 User is offline   ^淡如清风 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-August 12

Posted 12 August 2012 - 01:40 AM

不知道如何回复的好
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users