Browse Source

Fix categories parent id in SQL

Vova Tkach 5 years ago
parent
commit
13318ccc08

+ 17 - 11
engine/fetdata/blog.go

@@ -372,8 +372,8 @@ func (this *Blog) Categories(parent, depth int) []*BlogCategory {
 				main.alias,
 				main.alias,
 				main.lft,
 				main.lft,
 				main.rgt,
 				main.rgt,
-				depth.depth,
-				MAX(main.parent_id) AS parent_id
+				main.depth,
+				parent.id AS parent_id
 			FROM
 			FROM
 				(
 				(
 					SELECT
 					SELECT
@@ -383,20 +383,26 @@ func (this *Blog) Categories(parent, depth int) []*BlogCategory {
 						node.alias,
 						node.alias,
 						node.lft,
 						node.lft,
 						node.rgt,
 						node.rgt,
-						parent.id AS parent_id
+						(COUNT(parent.id) - 1) AS depth
 					FROM
 					FROM
 						blog_cats AS node,
 						blog_cats AS node,
 						blog_cats AS parent
 						blog_cats AS parent
 					WHERE
 					WHERE
-						node.lft BETWEEN parent.lft AND parent.rgt AND
-						node.id > 1
+						node.lft BETWEEN parent.lft AND parent.rgt
+					GROUP BY
+						node.id
 					ORDER BY
 					ORDER BY
 						node.lft ASC
 						node.lft ASC
 				) AS main
 				) AS main
 				LEFT JOIN (
 				LEFT JOIN (
 					SELECT
 					SELECT
 						node.id,
 						node.id,
-						(COUNT(parent.id) - 1) AS depth
+						node.user,
+						node.name,
+						node.alias,
+						node.lft,
+						node.rgt,
+						(COUNT(parent.id) - 0) AS depth
 					FROM
 					FROM
 						blog_cats AS node,
 						blog_cats AS node,
 						blog_cats AS parent
 						blog_cats AS parent
@@ -406,12 +412,12 @@ func (this *Blog) Categories(parent, depth int) []*BlogCategory {
 						node.id
 						node.id
 					ORDER BY
 					ORDER BY
 						node.lft ASC
 						node.lft ASC
-				) AS depth ON depth.id = main.id
+				) AS parent ON
+				parent.depth = main.depth AND
+				main.lft > parent.lft AND
+				main.rgt < parent.rgt
 			WHERE
 			WHERE
-				main.id > 1 AND
-				main.id <> main.parent_id
-			GROUP BY
-				main.id
+				main.id > 1
 			ORDER BY
 			ORDER BY
 				main.lft ASC
 				main.lft ASC
 			;
 			;

+ 35 - 21
engine/fetdata/blog_category.go

@@ -33,8 +33,8 @@ func (this *BlogCategory) load(cache *map[int]*utils.MySql_blog_category) *BlogC
 			main.alias,
 			main.alias,
 			main.lft,
 			main.lft,
 			main.rgt,
 			main.rgt,
-			depth.depth,
-			MAX(main.parent_id) AS parent_id
+			main.depth,
+			parent.id AS parent_id
 		FROM
 		FROM
 			(
 			(
 				SELECT
 				SELECT
@@ -44,20 +44,26 @@ func (this *BlogCategory) load(cache *map[int]*utils.MySql_blog_category) *BlogC
 					node.alias,
 					node.alias,
 					node.lft,
 					node.lft,
 					node.rgt,
 					node.rgt,
-					parent.id AS parent_id
+					(COUNT(parent.id) - 1) AS depth
 				FROM
 				FROM
 					blog_cats AS node,
 					blog_cats AS node,
 					blog_cats AS parent
 					blog_cats AS parent
 				WHERE
 				WHERE
-					node.lft BETWEEN parent.lft AND parent.rgt AND
-					node.id > 1
+					node.lft BETWEEN parent.lft AND parent.rgt
+				GROUP BY
+					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
 			) AS main
 			) AS main
 			LEFT JOIN (
 			LEFT JOIN (
 				SELECT
 				SELECT
 					node.id,
 					node.id,
-					(COUNT(parent.id) - 1) AS depth
+					node.user,
+					node.name,
+					node.alias,
+					node.lft,
+					node.rgt,
+					(COUNT(parent.id) - 0) AS depth
 				FROM
 				FROM
 					blog_cats AS node,
 					blog_cats AS node,
 					blog_cats AS parent
 					blog_cats AS parent
@@ -67,12 +73,12 @@ func (this *BlogCategory) load(cache *map[int]*utils.MySql_blog_category) *BlogC
 					node.id
 					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
-			) AS depth ON depth.id = main.id
+			) AS parent ON
+			parent.depth = main.depth AND
+			main.lft > parent.lft AND
+			main.rgt < parent.rgt
 		WHERE
 		WHERE
-			main.id > 1 AND
-			main.id <> main.parent_id
-		GROUP BY
-			main.id
+			main.id > 1
 		ORDER BY
 		ORDER BY
 			main.lft ASC
 			main.lft ASC
 		;
 		;
@@ -113,8 +119,8 @@ func (this *BlogCategory) loadById(id int) {
 			main.alias,
 			main.alias,
 			main.lft,
 			main.lft,
 			main.rgt,
 			main.rgt,
-			depth.depth,
-			MAX(main.parent_id) AS parent_id
+			main.depth,
+			parent.id AS parent_id
 		FROM
 		FROM
 			(
 			(
 				SELECT
 				SELECT
@@ -124,20 +130,26 @@ func (this *BlogCategory) loadById(id int) {
 					node.alias,
 					node.alias,
 					node.lft,
 					node.lft,
 					node.rgt,
 					node.rgt,
-					parent.id AS parent_id
+					(COUNT(parent.id) - 1) AS depth
 				FROM
 				FROM
 					blog_cats AS node,
 					blog_cats AS node,
 					blog_cats AS parent
 					blog_cats AS parent
 				WHERE
 				WHERE
-					node.lft BETWEEN parent.lft AND parent.rgt AND
-					node.id > 1
+					node.lft BETWEEN parent.lft AND parent.rgt
+				GROUP BY
+					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
 			) AS main
 			) AS main
 			LEFT JOIN (
 			LEFT JOIN (
 				SELECT
 				SELECT
 					node.id,
 					node.id,
-					(COUNT(parent.id) - 1) AS depth
+					node.user,
+					node.name,
+					node.alias,
+					node.lft,
+					node.rgt,
+					(COUNT(parent.id) - 0) AS depth
 				FROM
 				FROM
 					blog_cats AS node,
 					blog_cats AS node,
 					blog_cats AS parent
 					blog_cats AS parent
@@ -147,13 +159,15 @@ func (this *BlogCategory) loadById(id int) {
 					node.id
 					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
-			) AS depth ON depth.id = main.id
+			) AS parent ON
+			parent.depth = main.depth AND
+			main.lft > parent.lft AND
+			main.rgt < parent.rgt
 		WHERE
 		WHERE
 			main.id > 1 AND
 			main.id > 1 AND
-			main.id <> main.parent_id AND
 			main.id = ?
 			main.id = ?
-		GROUP BY
-			main.id
+		ORDER BY
+			main.lft ASC
 		;`,
 		;`,
 		id,
 		id,
 	).Scan(
 	).Scan(

+ 55 - 33
engine/fetdata/shop.go

@@ -92,8 +92,8 @@ func (this *Shop) load() *Shop {
 					main.alias,
 					main.alias,
 					main.lft,
 					main.lft,
 					main.rgt,
 					main.rgt,
-					depth.depth,
-					MAX(main.parent_id) AS parent_id
+					main.depth,
+					parent.id AS parent_id
 				FROM
 				FROM
 					(
 					(
 						SELECT
 						SELECT
@@ -103,20 +103,26 @@ func (this *Shop) load() *Shop {
 							node.alias,
 							node.alias,
 							node.lft,
 							node.lft,
 							node.rgt,
 							node.rgt,
-							parent.id AS parent_id
+							(COUNT(parent.id) - 1) AS depth
 						FROM
 						FROM
 							shop_cats AS node,
 							shop_cats AS node,
 							shop_cats AS parent
 							shop_cats AS parent
 						WHERE
 						WHERE
-							node.lft BETWEEN parent.lft AND parent.rgt AND
-							node.id > 1
+							node.lft BETWEEN parent.lft AND parent.rgt
+						GROUP BY
+							node.id
 						ORDER BY
 						ORDER BY
 							node.lft ASC
 							node.lft ASC
 					) AS main
 					) AS main
 					LEFT JOIN (
 					LEFT JOIN (
 						SELECT
 						SELECT
 							node.id,
 							node.id,
-							(COUNT(parent.id) - 1) AS depth
+							node.user,
+							node.name,
+							node.alias,
+							node.lft,
+							node.rgt,
+							(COUNT(parent.id) - 0) AS depth
 						FROM
 						FROM
 							shop_cats AS node,
 							shop_cats AS node,
 							shop_cats AS parent
 							shop_cats AS parent
@@ -126,12 +132,14 @@ func (this *Shop) load() *Shop {
 							node.id
 							node.id
 						ORDER BY
 						ORDER BY
 							node.lft ASC
 							node.lft ASC
-					) AS depth ON depth.id = main.id
+					) AS parent ON
+					parent.depth = main.depth AND
+					main.lft > parent.lft AND
+					main.rgt < parent.rgt
 				WHERE
 				WHERE
-					main.id > 1 AND
-					main.id <> main.parent_id
-				GROUP BY
-					main.id
+					main.id > 1
+				ORDER BY
+					main.lft ASC
 			) AS cats ON cats.id = shop_products.category
 			) AS cats ON cats.id = shop_products.category
 		WHERE
 		WHERE
 			shop_products.active = 1
 			shop_products.active = 1
@@ -233,8 +241,8 @@ func (this *Shop) load() *Shop {
 						main.alias,
 						main.alias,
 						main.lft,
 						main.lft,
 						main.rgt,
 						main.rgt,
-						depth.depth,
-						MAX(main.parent_id) AS parent_id
+						main.depth,
+						parent.id AS parent_id
 					FROM
 					FROM
 						(
 						(
 							SELECT
 							SELECT
@@ -244,20 +252,26 @@ func (this *Shop) load() *Shop {
 								node.alias,
 								node.alias,
 								node.lft,
 								node.lft,
 								node.rgt,
 								node.rgt,
-								parent.id AS parent_id
+								(COUNT(parent.id) - 1) AS depth
 							FROM
 							FROM
 								shop_cats AS node,
 								shop_cats AS node,
 								shop_cats AS parent
 								shop_cats AS parent
 							WHERE
 							WHERE
-								node.lft BETWEEN parent.lft AND parent.rgt AND
-								node.id > 1
+								node.lft BETWEEN parent.lft AND parent.rgt
+							GROUP BY
+								node.id
 							ORDER BY
 							ORDER BY
 								node.lft ASC
 								node.lft ASC
 						) AS main
 						) AS main
 						LEFT JOIN (
 						LEFT JOIN (
 							SELECT
 							SELECT
 								node.id,
 								node.id,
-								(COUNT(parent.id) - 1) AS depth
+								node.user,
+								node.name,
+								node.alias,
+								node.lft,
+								node.rgt,
+								(COUNT(parent.id) - 0) AS depth
 							FROM
 							FROM
 								shop_cats AS node,
 								shop_cats AS node,
 								shop_cats AS parent
 								shop_cats AS parent
@@ -267,12 +281,14 @@ func (this *Shop) load() *Shop {
 								node.id
 								node.id
 							ORDER BY
 							ORDER BY
 								node.lft ASC
 								node.lft ASC
-						) AS depth ON depth.id = main.id
+						) AS parent ON
+						parent.depth = main.depth AND
+						main.lft > parent.lft AND
+						main.rgt < parent.rgt
 					WHERE
 					WHERE
-						main.id > 1 AND
-						main.id <> main.parent_id
-					GROUP BY
-						main.id
+						main.id > 1
+					ORDER BY
+						main.lft ASC
 				) AS cats ON cats.id = shop_products.category
 				) AS cats ON cats.id = shop_products.category
 			WHERE
 			WHERE
 				shop_products.active = 1 AND
 				shop_products.active = 1 AND
@@ -566,8 +582,8 @@ func (this *Shop) Categories(parent, depth int) []*ShopCategory {
 				main.alias,
 				main.alias,
 				main.lft,
 				main.lft,
 				main.rgt,
 				main.rgt,
-				depth.depth,
-				MAX(main.parent_id) AS parent_id
+				main.depth,
+				parent.id AS parent_id
 			FROM
 			FROM
 				(
 				(
 					SELECT
 					SELECT
@@ -577,20 +593,26 @@ func (this *Shop) Categories(parent, depth int) []*ShopCategory {
 						node.alias,
 						node.alias,
 						node.lft,
 						node.lft,
 						node.rgt,
 						node.rgt,
-						parent.id AS parent_id
+						(COUNT(parent.id) - 1) AS depth
 					FROM
 					FROM
 						shop_cats AS node,
 						shop_cats AS node,
 						shop_cats AS parent
 						shop_cats AS parent
 					WHERE
 					WHERE
-						node.lft BETWEEN parent.lft AND parent.rgt AND
-						node.id > 1
+						node.lft BETWEEN parent.lft AND parent.rgt
+					GROUP BY
+						node.id
 					ORDER BY
 					ORDER BY
 						node.lft ASC
 						node.lft ASC
 				) AS main
 				) AS main
 				LEFT JOIN (
 				LEFT JOIN (
 					SELECT
 					SELECT
 						node.id,
 						node.id,
-						(COUNT(parent.id) - 1) AS depth
+						node.user,
+						node.name,
+						node.alias,
+						node.lft,
+						node.rgt,
+						(COUNT(parent.id) - 0) AS depth
 					FROM
 					FROM
 						shop_cats AS node,
 						shop_cats AS node,
 						shop_cats AS parent
 						shop_cats AS parent
@@ -600,12 +622,12 @@ func (this *Shop) Categories(parent, depth int) []*ShopCategory {
 						node.id
 						node.id
 					ORDER BY
 					ORDER BY
 						node.lft ASC
 						node.lft ASC
-				) AS depth ON depth.id = main.id
+				) AS parent ON
+				parent.depth = main.depth AND
+				main.lft > parent.lft AND
+				main.rgt < parent.rgt
 			WHERE
 			WHERE
-				main.id > 1 AND
-				main.id <> main.parent_id
-			GROUP BY
-				main.id
+				main.id > 1
 			ORDER BY
 			ORDER BY
 				main.lft ASC
 				main.lft ASC
 			;
 			;

+ 35 - 21
engine/fetdata/shop_category.go

@@ -33,8 +33,8 @@ func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopC
 			main.alias,
 			main.alias,
 			main.lft,
 			main.lft,
 			main.rgt,
 			main.rgt,
-			depth.depth,
-			MAX(main.parent_id) AS parent_id
+			main.depth,
+			parent.id AS parent_id
 		FROM
 		FROM
 			(
 			(
 				SELECT
 				SELECT
@@ -44,20 +44,26 @@ func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopC
 					node.alias,
 					node.alias,
 					node.lft,
 					node.lft,
 					node.rgt,
 					node.rgt,
-					parent.id AS parent_id
+					(COUNT(parent.id) - 1) AS depth
 				FROM
 				FROM
 					shop_cats AS node,
 					shop_cats AS node,
 					shop_cats AS parent
 					shop_cats AS parent
 				WHERE
 				WHERE
-					node.lft BETWEEN parent.lft AND parent.rgt AND
-					node.id > 1
+					node.lft BETWEEN parent.lft AND parent.rgt
+				GROUP BY
+					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
 			) AS main
 			) AS main
 			LEFT JOIN (
 			LEFT JOIN (
 				SELECT
 				SELECT
 					node.id,
 					node.id,
-					(COUNT(parent.id) - 1) AS depth
+					node.user,
+					node.name,
+					node.alias,
+					node.lft,
+					node.rgt,
+					(COUNT(parent.id) - 0) AS depth
 				FROM
 				FROM
 					shop_cats AS node,
 					shop_cats AS node,
 					shop_cats AS parent
 					shop_cats AS parent
@@ -67,12 +73,12 @@ func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopC
 					node.id
 					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
-			) AS depth ON depth.id = main.id
+			) AS parent ON
+			parent.depth = main.depth AND
+			main.lft > parent.lft AND
+			main.rgt < parent.rgt
 		WHERE
 		WHERE
-			main.id > 1 AND
-			main.id <> main.parent_id
-		GROUP BY
-			main.id
+			main.id > 1
 		ORDER BY
 		ORDER BY
 			main.lft ASC
 			main.lft ASC
 		;
 		;
@@ -113,8 +119,8 @@ func (this *ShopCategory) loadById(id int) {
 			main.alias,
 			main.alias,
 			main.lft,
 			main.lft,
 			main.rgt,
 			main.rgt,
-			depth.depth,
-			MAX(main.parent_id) AS parent_id
+			main.depth,
+			parent.id AS parent_id
 		FROM
 		FROM
 			(
 			(
 				SELECT
 				SELECT
@@ -124,20 +130,26 @@ func (this *ShopCategory) loadById(id int) {
 					node.alias,
 					node.alias,
 					node.lft,
 					node.lft,
 					node.rgt,
 					node.rgt,
-					parent.id AS parent_id
+					(COUNT(parent.id) - 1) AS depth
 				FROM
 				FROM
 					shop_cats AS node,
 					shop_cats AS node,
 					shop_cats AS parent
 					shop_cats AS parent
 				WHERE
 				WHERE
-					node.lft BETWEEN parent.lft AND parent.rgt AND
-					node.id > 1
+					node.lft BETWEEN parent.lft AND parent.rgt
+				GROUP BY
+					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
 			) AS main
 			) AS main
 			LEFT JOIN (
 			LEFT JOIN (
 				SELECT
 				SELECT
 					node.id,
 					node.id,
-					(COUNT(parent.id) - 1) AS depth
+					node.user,
+					node.name,
+					node.alias,
+					node.lft,
+					node.rgt,
+					(COUNT(parent.id) - 0) AS depth
 				FROM
 				FROM
 					shop_cats AS node,
 					shop_cats AS node,
 					shop_cats AS parent
 					shop_cats AS parent
@@ -147,13 +159,15 @@ func (this *ShopCategory) loadById(id int) {
 					node.id
 					node.id
 				ORDER BY
 				ORDER BY
 					node.lft ASC
 					node.lft ASC
-			) AS depth ON depth.id = main.id
+			) AS parent ON
+			parent.depth = main.depth AND
+			main.lft > parent.lft AND
+			main.rgt < parent.rgt
 		WHERE
 		WHERE
 			main.id > 1 AND
 			main.id > 1 AND
-			main.id <> main.parent_id AND
 			main.id = ?
 			main.id = ?
-		GROUP BY
-			main.id
+		ORDER BY
+			main.lft ASC
 		;`,
 		;`,
 		id,
 		id,
 	).Scan(
 	).Scan(

+ 18 - 10
modules/module_blog.go

@@ -43,8 +43,8 @@ func (this *Modules) RegisterModule_Blog() *Module {
 					main.alias,
 					main.alias,
 					main.lft,
 					main.lft,
 					main.rgt,
 					main.rgt,
-					depth.depth,
-					MAX(main.parent_id) AS parent_id
+					main.depth,
+					parent.id AS parent_id
 				FROM
 				FROM
 					(
 					(
 						SELECT
 						SELECT
@@ -54,20 +54,26 @@ func (this *Modules) RegisterModule_Blog() *Module {
 							node.alias,
 							node.alias,
 							node.lft,
 							node.lft,
 							node.rgt,
 							node.rgt,
-							parent.id AS parent_id
+							(COUNT(parent.id) - 1) AS depth
 						FROM
 						FROM
 							blog_cats AS node,
 							blog_cats AS node,
 							blog_cats AS parent
 							blog_cats AS parent
 						WHERE
 						WHERE
-							node.lft BETWEEN parent.lft AND parent.rgt AND
-							node.id > 1
+							node.lft BETWEEN parent.lft AND parent.rgt
+						GROUP BY
+							node.id
 						ORDER BY
 						ORDER BY
 							node.lft ASC
 							node.lft ASC
 					) AS main
 					) AS main
 					LEFT JOIN (
 					LEFT JOIN (
 						SELECT
 						SELECT
 							node.id,
 							node.id,
-							(COUNT(parent.id) - 1) AS depth
+							node.user,
+							node.name,
+							node.alias,
+							node.lft,
+							node.rgt,
+							(COUNT(parent.id) - 0) AS depth
 						FROM
 						FROM
 							blog_cats AS node,
 							blog_cats AS node,
 							blog_cats AS parent
 							blog_cats AS parent
@@ -77,13 +83,15 @@ func (this *Modules) RegisterModule_Blog() *Module {
 							node.id
 							node.id
 						ORDER BY
 						ORDER BY
 							node.lft ASC
 							node.lft ASC
-					) AS depth ON depth.id = main.id
+					) AS parent ON
+					parent.depth = main.depth AND
+					main.lft > parent.lft AND
+					main.rgt < parent.rgt
 				WHERE
 				WHERE
 					main.id > 1 AND
 					main.id > 1 AND
-					main.id <> main.parent_id AND
 					main.alias = ?
 					main.alias = ?
-				GROUP BY
-					main.id
+				ORDER BY
+					main.lft ASC
 				;`,
 				;`,
 				wrap.UrlArgs[2],
 				wrap.UrlArgs[2],
 			).Scan(
 			).Scan(

+ 18 - 10
modules/module_shop.go

@@ -285,8 +285,8 @@ func (this *Modules) RegisterModule_Shop() *Module {
 					main.alias,
 					main.alias,
 					main.lft,
 					main.lft,
 					main.rgt,
 					main.rgt,
-					depth.depth,
-					MAX(main.parent_id) AS parent_id
+					main.depth,
+					parent.id AS parent_id
 				FROM
 				FROM
 					(
 					(
 						SELECT
 						SELECT
@@ -296,20 +296,26 @@ func (this *Modules) RegisterModule_Shop() *Module {
 							node.alias,
 							node.alias,
 							node.lft,
 							node.lft,
 							node.rgt,
 							node.rgt,
-							parent.id AS parent_id
+							(COUNT(parent.id) - 1) AS depth
 						FROM
 						FROM
 							shop_cats AS node,
 							shop_cats AS node,
 							shop_cats AS parent
 							shop_cats AS parent
 						WHERE
 						WHERE
-							node.lft BETWEEN parent.lft AND parent.rgt AND
-							node.id > 1
+							node.lft BETWEEN parent.lft AND parent.rgt
+						GROUP BY
+							node.id
 						ORDER BY
 						ORDER BY
 							node.lft ASC
 							node.lft ASC
 					) AS main
 					) AS main
 					LEFT JOIN (
 					LEFT JOIN (
 						SELECT
 						SELECT
 							node.id,
 							node.id,
-							(COUNT(parent.id) - 1) AS depth
+							node.user,
+							node.name,
+							node.alias,
+							node.lft,
+							node.rgt,
+							(COUNT(parent.id) - 0) AS depth
 						FROM
 						FROM
 							shop_cats AS node,
 							shop_cats AS node,
 							shop_cats AS parent
 							shop_cats AS parent
@@ -319,13 +325,15 @@ func (this *Modules) RegisterModule_Shop() *Module {
 							node.id
 							node.id
 						ORDER BY
 						ORDER BY
 							node.lft ASC
 							node.lft ASC
-					) AS depth ON depth.id = main.id
+					) AS parent ON
+					parent.depth = main.depth AND
+					main.lft > parent.lft AND
+					main.rgt < parent.rgt
 				WHERE
 				WHERE
 					main.id > 1 AND
 					main.id > 1 AND
-					main.id <> main.parent_id AND
 					main.alias = ?
 					main.alias = ?
-				GROUP BY
-					main.id
+				ORDER BY
+					main.lft ASC
 				;`,
 				;`,
 				wrap.UrlArgs[2],
 				wrap.UrlArgs[2],
 			).Scan(
 			).Scan(