PartKeepr\CoreBundle\DoctrineMigrations\Version20150724174030::up PHP Method

up() public method

public up ( Doctrine\DBAL\Schema\Schema $schema )
$schema Doctrine\DBAL\Schema\Schema
    public function up(Schema $schema)
    {
        $this->performDatabaseUpgrade();
        $repository = $this->getEM()->getRepository('PartKeeprStorageLocationBundle:StorageLocationCategory');
        $rootNodes = $repository->getRootNodes();
        if (count($rootNodes) === 0) {
            // Ensure that the root category exists
            $rootNode = new StorageLocationCategory();
            $rootNode->setName('Root Node');
            $this->getEM()->persist($rootNode);
            $this->getEM()->flush();
        } else {
            $rootNode = array_values($rootNodes)[0];
        }
        $storageLocationRepository = $this->getEM()->getRepository('PartKeeprStorageLocationBundle:StorageLocation');
        $allStorageLocations = $storageLocationRepository->findAll();
        foreach ($allStorageLocations as $storageLocation) {
            $storageLocation->setCategory($rootNode);
        }
        $this->getEM()->flush();
    }
Version20150724174030