Version20150724174030.php (1538B)
1 <?php 2 3 namespace PartKeepr\CoreBundle\DoctrineMigrations; 4 5 use Doctrine\DBAL\Schema\Schema; 6 use PartKeepr\StorageLocationBundle\Entity\StorageLocationCategory; 7 8 /** 9 * Migrates all storage locations to support categories. Creates a new root category if it doesn't exist, then applies 10 * the root category to all storage locations. 11 */ 12 class Version20150724174030 extends BaseMigration 13 { 14 /** 15 * @param Schema $schema 16 */ 17 public function up(Schema $schema) 18 { 19 $this->performDatabaseUpgrade(); 20 21 $repository = $this->getEM()->getRepository( 22 'PartKeeprStorageLocationBundle:StorageLocationCategory' 23 ); 24 25 $rootNodes = $repository->getRootNodes(); 26 27 if (count($rootNodes) === 0) { 28 // Ensure that the root category exists 29 $rootNode = new StorageLocationCategory(); 30 $rootNode->setName('Root Node'); 31 $this->getEM()->persist($rootNode); 32 $this->getEM()->flush(); 33 } else { 34 $rootNode = array_values($rootNodes)[0]; 35 } 36 37 $storageLocationRepository = $this->getEM()->getRepository( 38 'PartKeeprStorageLocationBundle:StorageLocation' 39 ); 40 41 $allStorageLocations = $storageLocationRepository->findAll(); 42 43 foreach ($allStorageLocations as $storageLocation) { 44 $storageLocation->setCategory($rootNode); 45 } 46 $this->getEM()->flush(); 47 } 48 49 /** 50 * @param Schema $schema 51 */ 52 public function down(Schema $schema) 53 { 54 } 55 }