Browse Source

Shop products (category parent)

Vova Tkach 5 years ago
parent
commit
0e5cdaf449
1 changed files with 48 additions and 10 deletions
  1. 48 10
      modules/module_shop.go

+ 48 - 10
modules/module_shop.go

@@ -279,18 +279,54 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			row := &utils.MySql_shop_category{}
 			err := wrap.DB.QueryRow(`
 				SELECT
-					id,
-					user,
-					name,
-					alias,
-					lft,
-					rgt
+					main.id,
+					main.user,
+					main.name,
+					main.alias,
+					main.lft,
+					main.rgt,
+					depth.depth,
+					MAX(main.parent_id) AS parent_id
 				FROM
-					shop_cats
+					(
+						SELECT
+							node.id,
+							node.user,
+							node.name,
+							node.alias,
+							node.lft,
+							node.rgt,
+							parent.id AS parent_id
+						FROM
+							shop_cats AS node,
+							shop_cats AS parent
+						WHERE
+							node.lft BETWEEN parent.lft AND parent.rgt AND
+							node.id > 1
+						ORDER BY
+							node.lft ASC
+					) AS main
+					LEFT JOIN (
+						SELECT
+							node.id,
+							(COUNT(parent.id) - 1) AS depth
+						FROM
+							shop_cats AS node,
+							shop_cats AS parent
+						WHERE
+							node.lft BETWEEN parent.lft AND parent.rgt
+						GROUP BY
+							node.id
+						ORDER BY
+							node.lft ASC
+					) AS depth ON depth.id = main.id
 				WHERE
-					alias = ? AND
-					id > 1
-				LIMIT 1;`,
+					main.id > 1 AND
+					main.id <> main.parent_id AND
+					main.alias = ?
+				GROUP BY
+					main.id
+				;`,
 				wrap.UrlArgs[2],
 			).Scan(
 				&row.A_id,
@@ -299,6 +335,8 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				&row.A_alias,
 				&row.A_lft,
 				&row.A_rgt,
+				&row.A_depth,
+				&row.A_parent,
 			)
 
 			if err != nil && err != wrapper.ErrNoRows {