public static function getCreateTable($recordClass, $historyVariant = false)
    {
        $indexes = $historyVariant ? [] : $recordClass::$indexes;
        $fulltextColumns = [];
        $queryString = [];


        // history table revisionID field
        if ($historyVariant) {
            $queryString[] = '`RevisionID` int(10) unsigned NOT NULL auto_increment';
            $queryString[] = 'PRIMARY KEY (`RevisionID`)';
        }

        $queryString = array_merge($queryString, static::compileFields($recordClass, $historyVariant));

        if (!$historyVariant) {
            // If ContextClass && ContextID are members of this model let's index them
            if ($recordClass::fieldExists('ContextClass') && $recordClass::fieldExists('ContextID')) {
                $queryString[] = static::getContextIndex($recordClass);
            }

            $fulltextColumns = static::getFullTextColumns($recordClass);
        }

        // compile indexes
        foreach ($indexes as $indexName => $index) {

            // translate field names
            foreach ($index['fields'] as &$indexField) {
                $indexField = $recordClass::getColumnName($indexField);
            }

            if (!empty($index['fulltext'])) {
                $fulltextColumns = array_unique(array_merge($fulltextColumns, $index['fields']));
                continue;
            }

            $queryString[] = sprintf(
                '%s KEY `%s` (`%s`)',
                !empty($index['unique']) ? 'UNIQUE' : '',
                $indexName,
                join('`,`', $index['fields'])
            );
        }

        if (!empty($fulltextColumns)) {
            $queryString[] = 'FULLTEXT KEY `FULLTEXT` (`'.join('`,`', $fulltextColumns).'`)';
        }


        $createSQL = sprintf(
            "CREATE TABLE IF NOT EXISTS `%s` (\n\t%s\n) ENGINE=MyISAM DEFAULT CHARSET=utf8;",
            $historyVariant ? $recordClass::getHistoryTable() : $recordClass::$tableName,
            join("\n\t,", $queryString)
        );

        // append history table SQL
        if (!$historyVariant && is_subclass_of($recordClass, 'VersionedRecord')) {
            $createSQL .= PHP_EOL.PHP_EOL.PHP_EOL.static::getCreateTable($recordClass, true);
        }
        return $createSQL;
    }
public static function getCreateTable($recordClass, $historyVariant = false)
    {
        $indexes = $historyVariant ? [] : $recordClass::$indexes;
        $fulltextColumns = [];
        $queryString = [];


        // history table revisionID field
        if ($historyVariant) {
            $queryString[] = '`RevisionID` int(10) unsigned NOT NULL auto_increment';
            $queryString[] = 'PRIMARY KEY (`RevisionID`)';
        }

        $queryString = array_merge($queryString, static::compileFields($recordClass, $historyVariant));

        if (!$historyVariant) {
            // If ContextClass && ContextID are members of this model let's index them
            if ($recordClass::fieldExists('ContextClass') && $recordClass::fieldExists('ContextID')) {
                $queryString[] = static::getContextIndex($recordClass);
            }

            $fulltextColumns = static::getFullTextColumns($recordClass);
        }

        // compile indexes
        foreach ($indexes as $indexName => $index) {

            // translate field names
            foreach ($index['fields'] as &$indexField) {
                $indexField = $recordClass::getColumnName($indexField);
            }

            if (!empty($index['fulltext'])) {
                $fulltextColumns = array_unique(array_merge($fulltextColumns, $index['fields']));
                continue;
            }

            $queryString[] = sprintf(
                '%s KEY `%s` (`%s`)',
                !empty($index['unique']) ? 'UNIQUE' : '',
                $indexName,
                join('`,`', $index['fields'])
            );
        }

        if (!empty($fulltextColumns)) {
            $queryString[] = 'FULLTEXT KEY `FULLTEXT` (`'.join('`,`', $fulltextColumns).'`)';
        }


        $createSQL = sprintf(
            "CREATE TABLE IF NOT EXISTS `%s` (\n\t%s\n) ENGINE=MyISAM DEFAULT CHARSET=utf8;",
            $historyVariant ? $recordClass::getHistoryTable() : $recordClass::$tableName,
            join("\n\t,", $queryString)
        );

        // append history table SQL
        if (!$historyVariant && is_subclass_of($recordClass, 'VersionedRecord')) {
            $createSQL .= PHP_EOL.PHP_EOL.PHP_EOL.static::getCreateTable($recordClass, true);
        }
        return $createSQL;
    }
public static function getCreateTable ( $ recordClass , $ historyVariant = false ) { $ indexes = $ historyVariant ? [ ] : $ recordClass :: $ indexes ; $ fulltextColumns = [ ] ; $ queryString = [ ] ; // history table revisionID field if ( $ historyVariant ) { $ queryString [ ] = '`RevisionID` int(10) unsigned NOT NULL auto_increment' ; $ queryString [ ] = 'PRIMARY KEY (`RevisionID`)' ; } $ queryString = array_merge ( $ queryString , static :: compileFields ( $ recordClass , $ historyVariant ) ) ; if ( ! $ historyVariant ) { // If ContextClass && ContextID are members of this model let's index them if ( $ recordClass :: fieldExists ( 'ContextClass' ) && $ recordClass :: fieldExists ( 'ContextID' ) ) { $ queryString [ ] = static :: getContextIndex ( $ recordClass ) ; } $ fulltextColumns = static :: getFullTextColumns ( $ recordClass ) ; } // compile indexes foreach ( $ indexes as $ indexName => $ index ) { // translate field names foreach ( $ index [ 'fields' ] as & $ indexField ) { $ indexField = $ recordClass :: getColumnName ( $ indexField ) ; } if ( ! empty ( $ index [ 'fulltext' ] ) ) { $ fulltextColumns = array_unique ( array_merge ( $ fulltextColumns , $ index [ 'fields' ] ) ) ; continue ; } $ queryString [ ] = sprintf ( '%s KEY `%s` (`%s`)' , ! empty ( $ index [ 'unique' ] ) ? 'UNIQUE' : '' , $ indexName , join ( '`,`' , $ index [ 'fields' ] ) ) ; } if ( ! empty ( $ fulltextColumns ) ) { $ queryString [ ] = 'FULLTEXT KEY `FULLTEXT` (`' . join ( '`,`' , $ fulltextColumns ) . '`)' ; } $ createSQL = sprintf ( "CREATE TABLE IF NOT EXISTS `%s` (\n\t%s\n) ENGINE=MyISAM DEFAULT CHARSET=utf8;" , $ historyVariant ? $ recordClass :: getHistoryTable ( ) : $ recordClass :: $ tableName , join ( "\n\t," , $ queryString ) ) ; // append history table SQL if ( ! $ historyVariant && is_subclass_of ( $ recordClass , 'VersionedRecord' ) ) { $ createSQL .= PHP_EOL . PHP_EOL . PHP_EOL . static :: getCreateTable ( $ recordClass , true ) ; } return $ createSQL ; }
Generates a MySQL create table query from a Divergence\Models\ActiveRecord class.

@param string $recordClass Class name
@param boolean $historyVariant
@return void
Generates a MySQL create table query from a Divergence \ Models \ ActiveRecord class .