partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

UpdateCategoryPathCommand.php (1948B)


      1 <?php
      2 
      3 namespace PartKeepr\CoreBundle\Command;
      4 
      5 use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
      6 use PartKeepr\CategoryBundle\Entity\CategoryPathInterface;
      7 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
      8 use Symfony\Component\Console\Input\InputInterface;
      9 use Symfony\Component\Console\Output\OutputInterface;
     10 
     11 class UpdateCategoryPathCommand extends ContainerAwareCommand
     12 {
     13     public function configure()
     14     {
     15         parent::configure();
     16         $this->setName('partkeepr:update-category-paths');
     17         $this->setDescription('Updates the category paths for all category trees');
     18     }
     19 
     20     public function execute(InputInterface $input, OutputInterface $output)
     21     {
     22         $entities = [
     23             'PartKeepr\FootprintBundle\Entity\FootprintCategory',
     24             'PartKeepr\PartBundle\Entity\PartCategory',
     25             'PartKeepr\StorageLocationBundle\Entity\StorageLocationCategory',
     26         ];
     27 
     28         foreach ($entities as $entity) {
     29             $this->regenerateCategoryPaths($entity);
     30         }
     31     }
     32 
     33     public function regenerateCategoryPaths($entity)
     34     {
     35         $repository = $this->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository($entity);
     36 
     37         /*
     38          * @var $repository NestedTreeRepository
     39          */
     40         $rootNodes = $repository->getRootNodes();
     41 
     42         $pathSeparator = $this->getContainer()->getParameter('partkeepr.category.path_separator');
     43 
     44         foreach ($rootNodes as $rootNode) {
     45             /*
     46              * @var $rootNode CategoryPathInterface
     47              */
     48             $rootNode->setCategoryPath(uniqid());
     49         }
     50 
     51         $this->getContainer()->get('doctrine.orm.default_entity_manager')->flush();
     52 
     53         foreach ($rootNodes as $rootNode) {
     54             $rootNode->setCategoryPath($rootNode->generateCategoryPath($pathSeparator));
     55         }
     56 
     57         $this->getContainer()->get('doctrine.orm.default_entity_manager')->flush();
     58     }
     59 }