src/Common/Console/Command/Make/Model.php
private function convertTablesToLocalised(\stdClass $oModel): self
    {
        /** @var Database $oDb */
        $oDb = Factory::service('Database');
        /** @var \Nails\Common\Service\Locale $locale */
        $oLocale = Factory::service('Locale');

        $aQueries = [
            //  Disable foreign key checks so we can shift the tables about
            'SET FOREIGN_KEY_CHECKS = 0;',

            //  Rename the existing table (and data)
            'RENAME TABLE `' . $oModel->table_with_prefix . '` TO `' . $oModel->table_with_prefix . '_localised`;',

            //  Drop the AUTO_INCREMENT key
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` CHANGE `id` `id` INT(11) UNSIGNED NOT NULL;',

            //  Change the primary key
            'ALTER TABLE  `' . $oModel->table_with_prefix . '_localised` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `language`, `region`)',

            //  Create the new top-level table
            'CREATE TABLE `' . $oModel->table_with_prefix . '` (id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT) DEFAULT CHARACTER SET `utf8`;',

            //  Add a foreign key to the localised table
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` ADD FOREIGN KEY (`id`) REFERENCES `' . $oModel->table_with_prefix . '` (`id`) ON DELETE CASCADE;',

            //  Add the locale columns
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` ADD `language` VARCHAR(5) NULL DEFAULT NULL AFTER `id`;',
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` ADD `region` VARCHAR(5) NULL DEFAULT NULL AFTER `language`;',

            //  Populate the top-level table
            'INSERT INTO ' . $oModel->table_with_prefix . ' SELECT id FROM ' . $oModel->table_with_prefix . '_localised;',

            //  Update the columns with the default language
            'UPDATE `' . $oModel->table_with_prefix . '_localised` SET `language` = "' . $oLocale->get()->getLanguage() . '"',
            'UPDATE `' . $oModel->table_with_prefix . '_localised` SET `region` = "' . $oLocale->get()->getRegion() . '"',

            //  Re-enable foreign key checks
            'SET FOREIGN_KEY_CHECKS = 1;',
        ];

        array_walk($aQueries, function ($sQuery) use ($oDb) {
            $oDb->query($sQuery);
        });

        $this->warning(array_merge(['Remember to add migrations!', ''], $aQueries));

        return $this;
    }
private function convertTablesToLocalised(\stdClass $oModel): self
    {
        /** @var Database $oDb */
        $oDb = Factory::service('Database');
        /** @var \Nails\Common\Service\Locale $locale */
        $oLocale = Factory::service('Locale');

        $aQueries = [
            //  Disable foreign key checks so we can shift the tables about
            'SET FOREIGN_KEY_CHECKS = 0;',

            //  Rename the existing table (and data)
            'RENAME TABLE `' . $oModel->table_with_prefix . '` TO `' . $oModel->table_with_prefix . '_localised`;',

            //  Drop the AUTO_INCREMENT key
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` CHANGE `id` `id` INT(11) UNSIGNED NOT NULL;',

            //  Change the primary key
            'ALTER TABLE  `' . $oModel->table_with_prefix . '_localised` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `language`, `region`)',

            //  Create the new top-level table
            'CREATE TABLE `' . $oModel->table_with_prefix . '` (id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT) DEFAULT CHARACTER SET `utf8`;',

            //  Add a foreign key to the localised table
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` ADD FOREIGN KEY (`id`) REFERENCES `' . $oModel->table_with_prefix . '` (`id`) ON DELETE CASCADE;',

            //  Add the locale columns
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` ADD `language` VARCHAR(5) NULL DEFAULT NULL AFTER `id`;',
            'ALTER TABLE `' . $oModel->table_with_prefix . '_localised` ADD `region` VARCHAR(5) NULL DEFAULT NULL AFTER `language`;',

            //  Populate the top-level table
            'INSERT INTO ' . $oModel->table_with_prefix . ' SELECT id FROM ' . $oModel->table_with_prefix . '_localised;',

            //  Update the columns with the default language
            'UPDATE `' . $oModel->table_with_prefix . '_localised` SET `language` = "' . $oLocale->get()->getLanguage() . '"',
            'UPDATE `' . $oModel->table_with_prefix . '_localised` SET `region` = "' . $oLocale->get()->getRegion() . '"',

            //  Re-enable foreign key checks
            'SET FOREIGN_KEY_CHECKS = 1;',
        ];

        array_walk($aQueries, function ($sQuery) use ($oDb) {
            $oDb->query($sQuery);
        });

        $this->warning(array_merge(['Remember to add migrations!', ''], $aQueries));

        return $this;
    }
private function convertTablesToLocalised ( \ stdClass $ oModel ) : self { /** @var Database $oDb */ $ oDb = Factory :: service ( 'Database' ) ; /** @var \Nails\Common\Service\Locale $locale */ $ oLocale = Factory :: service ( 'Locale' ) ; $ aQueries = [ //  Disable foreign key checks so we can shift the tables about 'SET FOREIGN_KEY_CHECKS = 0;' , //  Rename the existing table (and data) 'RENAME TABLE `' . $ oModel -> table_with_prefix . '` TO `' . $ oModel -> table_with_prefix . '_localised`;' , //  Drop the AUTO_INCREMENT key 'ALTER TABLE `' . $ oModel -> table_with_prefix . '_localised` CHANGE `id` `id` INT(11) UNSIGNED NOT NULL;' , //  Change the primary key 'ALTER TABLE  `' . $ oModel -> table_with_prefix . '_localised` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `language`, `region`)' , //  Create the new top-level table 'CREATE TABLE `' . $ oModel -> table_with_prefix . '` (id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT) DEFAULT CHARACTER SET `utf8`;' , //  Add a foreign key to the localised table 'ALTER TABLE `' . $ oModel -> table_with_prefix . '_localised` ADD FOREIGN KEY (`id`) REFERENCES `' . $ oModel -> table_with_prefix . '` (`id`) ON DELETE CASCADE;' , //  Add the locale columns 'ALTER TABLE `' . $ oModel -> table_with_prefix . '_localised` ADD `language` VARCHAR(5) NULL DEFAULT NULL AFTER `id`;' , 'ALTER TABLE `' . $ oModel -> table_with_prefix . '_localised` ADD `region` VARCHAR(5) NULL DEFAULT NULL AFTER `language`;' , //  Populate the top-level table 'INSERT INTO ' . $ oModel -> table_with_prefix . ' SELECT id FROM ' . $ oModel -> table_with_prefix . '_localised;' , //  Update the columns with the default language 'UPDATE `' . $ oModel -> table_with_prefix . '_localised` SET `language` = "' . $ oLocale -> get ( ) -> getLanguage ( ) . '"' , 'UPDATE `' . $ oModel -> table_with_prefix . '_localised` SET `region` = "' . $ oLocale -> get ( ) -> getRegion ( ) . '"' , //  Re-enable foreign key checks 'SET FOREIGN_KEY_CHECKS = 1;' , ] ; array_walk ( $ aQueries , function ( $ sQuery ) use ( $ oDb ) { $ oDb -> query ( $ sQuery ) ; } ) ; $ this -> warning ( array_merge ( [ 'Remember to add migrations!' , '' ] , $ aQueries ) ) ; return $ this ; }
Converts existing non-localised model tables to a localised version

@param \stdClass $oModel The model being converted

@return $this
Converts existing non - localised model tables to a localised version