-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shopsys] Change blog categories for real root category (#3595)
Co-authored-by: Michal Vanek <michal.vanek@shopsys.com>
- Loading branch information
Showing
9 changed files
with
113 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrameworkBundle\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Exception; | ||
use Gedmo\Tree\Entity\Repository\NestedTreeRepository; | ||
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory; | ||
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
|
||
class Version20241112100245 extends AbstractMigration implements ContainerAwareInterface | ||
{ | ||
use MultidomainMigrationTrait; | ||
|
||
/** | ||
* @param \Doctrine\DBAL\Schema\Schema $schema | ||
*/ | ||
public function up(Schema $schema): void | ||
{ | ||
$rootBlogCategories = $this->sql('SELECT id FROM blog_categories WHERE parent_id IS NULL AND level = 0')->fetchAllAssociative(); | ||
|
||
if (count($rootBlogCategories) === 1) { | ||
$blogCategoryId = reset($rootBlogCategories)['id']; | ||
$translation = $this->sql('SELECT 1 FROM blog_category_translations WHERE translatable_id = ?', [$blogCategoryId])->fetchOne(); | ||
|
||
if ($translation === false) { | ||
$this->sql('UPDATE blog_categories SET parent_id = NULL WHERE parent_id = ?', [$blogCategoryId]); | ||
$this->sql('DELETE FROM blog_category_domains WHERE blog_category_id = ?', [$blogCategoryId]); | ||
$this->sql('DELETE FROM blog_categories WHERE id = ?', [$blogCategoryId]); | ||
$this->sql('UPDATE blog_categories SET level = level -1'); | ||
} | ||
|
||
$entityManager = $this->container->get('doctrine.orm.default_entity_manager'); | ||
$repository = $entityManager->getRepository(BlogCategory::class); | ||
|
||
if ($repository instanceof NestedTreeRepository) { | ||
$repository->recover(); | ||
} | ||
} elseif (count($rootBlogCategories) > 1) { | ||
throw new Exception('There is more than one root blog category. Please see upgrade notes.'); | ||
} | ||
} | ||
|
||
/** | ||
* @param \Doctrine\DBAL\Schema\Schema $schema | ||
*/ | ||
public function down(Schema $schema): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Model/Blog/Category/Exception/RootLevelBlogCategoryAlreadyExistsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrameworkBundle\Model\Blog\Category\Exception; | ||
|
||
use Exception; | ||
|
||
class RootLevelBlogCategoryAlreadyExistsException extends Exception | ||
{ | ||
/** | ||
* @param int $blogCategoryId | ||
* @param \Exception|null $previous | ||
*/ | ||
public function __construct(int $blogCategoryId, ?Exception $previous = null) | ||
{ | ||
$message = sprintf('There can be only one blog category with root level. The current root level blog category ID is "%d".', $blogCategoryId); | ||
|
||
parent::__construct($message, 0, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters