diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php index 1470a4b43..f701f7b06 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php @@ -15,13 +15,14 @@ class AddStoredFunctionToGetUrlPathOfCategory extends Migration $functionSQL = <<< SQL DROP FUNCTION IF EXISTS `get_url_path_of_category`; CREATE FUNCTION get_url_path_of_category( - categoryId INT + categoryId INT, + localeCode VARCHAR(255) ) RETURNS VARCHAR(255) DETERMINISTIC BEGIN - DECLARE urlPath VARCHAR(255); + DECLARE urlPath VARCHAR(255); IF categoryId != 1 THEN SELECT @@ -35,6 +36,7 @@ class AddStoredFunctionToGetUrlPathOfCategory extends Migration AND node._rgt <= parent._rgt AND node.id = categoryId AND parent.id <> 1 + AND parent_translations.locale = localeCode GROUP BY node.id; diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php index 8004d61c8..244cd5e3c 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php @@ -15,23 +15,22 @@ class AddTriggerToCategoryTranslations extends Migration */ public function up() { - $insertTriggerBody = $this->getTriggerBody('insert'); + $triggerBody = $this->getTriggerBody(); $insertTrigger = <<< SQL CREATE TRIGGER %s BEFORE INSERT ON category_translations FOR EACH ROW BEGIN - $insertTriggerBody + $triggerBody END; SQL; - $updateTriggerBody = $this->getTriggerBody(); $updateTrigger = <<< SQL CREATE TRIGGER %s BEFORE UPDATE ON category_translations FOR EACH ROW BEGIN - $updateTriggerBody + $triggerBody END; SQL; @@ -54,26 +53,21 @@ SQL; } /** - * Returns trigger body as string based on type ('update' or 'insert'). - * - * @param string $type + * Returns trigger body as string * * @return string */ - private function getTriggerBody(string $type = 'update'): string + private function getTriggerBody() { - $addOnInsert = ($type === 'update') ? '' : <<< SQL - ELSE - SELECT CONCAT(urlPath, '/', NEW.slug) INTO urlPath; -SQL; - - return <<< SQL + return <<= parent._lft AND node._rgt <= parent._rgt - AND node.id = NEW.category_id + AND node.id = (SELECT parent_id FROM categories WHERE id = NEW.category_id) AND parent.id <> 1 + AND parent_translations.locale = NEW.locale GROUP BY node.id; - - IF urlPath IS NULL + + IF parentUrlPath IS NULL THEN SET urlPath = NEW.slug; - $addOnInsert + ELSE + SET urlPath = concat(parentUrlPath, '/', NEW.slug); END IF; SET NEW.url_path = urlPath; + END IF; -SQL; + SQL; + } } diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194648_add_url_path_to_existing_category_translations.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194648_add_url_path_to_existing_category_translations.php index accaf674f..9613bd079 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194648_add_url_path_to_existing_category_translations.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194648_add_url_path_to_existing_category_translations.php @@ -13,27 +13,18 @@ class AddUrlPathToExistingCategoryTranslations extends Migration */ public function up() { - $sql = <<< SQL - SELECT - GROUP_CONCAT(parent_translations.slug SEPARATOR '/') AS url_path - FROM - categories AS node, - categories AS parent - JOIN category_translations AS parent_translations ON parent.id = parent_translations.category_id - WHERE - node._lft >= parent._lft - AND node._rgt <= parent._rgt - AND node.id = :category_id - AND node.id <> 1 - AND parent.id <> 1 - GROUP BY - node.id -SQL; + $sqlStoredFunction = <<< SQL + SELECT get_url_path_of_category(:category_id, :locale_code) AS url_path; + SQL; + $categoryTranslationsTableName = app(CategoryTranslation::class)->getTable(); foreach (DB::table($categoryTranslationsTableName)->get() as $categoryTranslation) { - $urlPathQueryResult = DB::selectOne($sql, ['category_id' => $categoryTranslation->category_id]); + $urlPathQueryResult = DB::selectOne($sqlStoredFunction, [ + 'category_id' => $categoryTranslation->category_id, + 'locale_code' => $categoryTranslation->locale, + ]); $url_path = $urlPathQueryResult ? $urlPathQueryResult->url_path : ''; DB::table($categoryTranslationsTableName) diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php index e25243d64..0fb1e6af4 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php @@ -17,16 +17,39 @@ class AddTriggerToCategories extends Migration { $triggerBody = <<< SQL DECLARE urlPath VARCHAR(255); + DECLARE localeCode VARCHAR(255); + DECLARE done INT; + DECLARE curs CURSOR FOR (SELECT category_translations.locale + FROM category_translations + WHERE category_id = NEW.id); + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; + - SELECT get_url_path_of_category(NEW.id) INTO urlPath; - - IF EXISTS (SELECT * FROM category_translations WHERE category_id = NEW.id) + IF EXISTS ( + SELECT * + FROM category_translations + WHERE category_id = NEW.id + ) THEN - UPDATE category_translations - SET url_path = urlPath - WHERE category_translations.category_id = NEW.id; + + OPEN curs; + + SET done = 0; + REPEAT + FETCH curs INTO localeCode; + + SELECT get_url_path_of_category(NEW.id, localeCode) INTO urlPath; + + UPDATE category_translations + SET url_path = urlPath + WHERE category_translations.category_id = NEW.id; + + UNTIL done END REPEAT; + + CLOSE curs; + END IF; -SQL; + SQL; $insertTrigger = <<< SQL CREATE TRIGGER %s @@ -35,7 +58,7 @@ SQL; BEGIN $triggerBody END; -SQL; + SQL; $updateTrigger = <<< SQL CREATE TRIGGER %s @@ -44,7 +67,7 @@ SQL; BEGIN $triggerBody END; -SQL; + SQL; DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_INSERT)); DB::unprepared(sprintf($insertTrigger, self::TRIGGER_NAME_INSERT));