/** * Check the database schema and update it as necessary. * * @uses DataExtension->augmentDatabase() */ public function requireTable() { // Only build the table if we've actually got fields $fields = self::database_fields($this->class); $table = static::getSchema()->tableName($this->class); $extensions = self::database_extensions($this->class); $indexes = $this->databaseIndexes(); // Validate relationship configuration $this->validateModelDefinitions(); if ($fields) { $hasAutoIncPK = get_parent_class($this) === 'SilverStripe\\ORM\\DataObject'; DB::require_table($table, $fields, $indexes, $hasAutoIncPK, $this->stat('create_table_options'), $extensions); } else { DB::dont_require_table($table); } // Build any child tables for many_many items if ($manyMany = $this->uninherited('many_many')) { $extras = $this->uninherited('many_many_extraFields'); foreach ($manyMany as $relationship => $childClass) { // Build field list if ($this->class === $childClass) { $childField = "ChildID"; } else { $childTable = $this->getSchema()->tableName($childClass); $childField = "{$childTable}ID"; } $manymanyFields = array("{$table}ID" => "Int", $childField => "Int"); if (isset($extras[$relationship])) { $manymanyFields = array_merge($manymanyFields, $extras[$relationship]); } // Build index list $manymanyIndexes = array("{$table}ID" => true, $childField => true); $manyManyTable = "{$table}_{$relationship}"; DB::require_table($manyManyTable, $manymanyFields, $manymanyIndexes, true, null, $extensions); } } // Let any extentions make their own database fields $this->extend('augmentDatabase', $dummy); }
/** * Check the database schema and update it as necessary. * * @uses DataExtension->augmentDatabase() */ public function requireTable() { // Only build the table if we've actually got fields $schema = static::getSchema(); $fields = $schema->databaseFields(static::class, false); $table = $schema->tableName(static::class); $extensions = self::database_extensions(static::class); $indexes = $this->databaseIndexes(); if ($fields) { $hasAutoIncPK = get_parent_class($this) === self::class; DB::require_table($table, $fields, $indexes, $hasAutoIncPK, $this->stat('create_table_options'), $extensions); } else { DB::dont_require_table($table); } // Build any child tables for many_many items if ($manyMany = $this->uninherited('many_many')) { $extras = $this->uninherited('many_many_extraFields'); foreach ($manyMany as $component => $spec) { // Get many_many spec $manyManyComponent = $schema->manyManyComponent(static::class, $component); list($relationClass, $parentClass, $componentClass, $parentField, $childField, $tableOrClass) = $manyManyComponent; // Skip if backed by actual class if (class_exists($tableOrClass)) { continue; } // Build fields $manymanyFields = array($parentField => "Int", $childField => "Int"); if (isset($extras[$component])) { $manymanyFields = array_merge($manymanyFields, $extras[$component]); } // Build index list $manymanyIndexes = array($parentField => true, $childField => true); DB::require_table($tableOrClass, $manymanyFields, $manymanyIndexes, true, null, $extensions); } } // Let any extentions make their own database fields $this->extend('augmentDatabase', $dummy); }
public function augmentDatabase() { $owner = $this->owner; $class = get_class($owner); $baseTable = $this->baseTable(); $classTable = $owner->getSchema()->tableName($owner); $isRootClass = $class === $owner->baseClass(); // Build a list of suffixes whose tables need versioning $allSuffixes = array(); $versionableExtensions = $owner->config()->versionableExtensions; if (count($versionableExtensions)) { foreach ($versionableExtensions as $versionableExtension => $suffixes) { if ($owner->hasExtension($versionableExtension)) { foreach ((array) $suffixes as $suffix) { $allSuffixes[$suffix] = $versionableExtension; } } } } // Add the default table with an empty suffix to the list (table name = class name) $allSuffixes[''] = null; foreach ($allSuffixes as $suffix => $extension) { // Check tables for this build if ($suffix) { $suffixBaseTable = "{$baseTable}_{$suffix}"; $suffixTable = "{$classTable}_{$suffix}"; } else { $suffixBaseTable = $baseTable; $suffixTable = $classTable; } $fields = DataObject::database_fields($owner->class); unset($fields['ID']); if ($fields) { $options = Config::inst()->get($owner->class, 'create_table_options', Config::FIRST_SET); $indexes = $owner->databaseIndexes(); $extensionClass = $allSuffixes[$suffix]; if ($suffix && ($extension = $owner->getExtensionInstance($extensionClass))) { if (!$extension instanceof VersionableExtension) { throw new LogicException("Extension {$extensionClass} must implement VersionableExtension"); } // Allow versionable extension to customise table fields and indexes $extension->setOwner($owner); if ($extension->isVersionedTable($suffixTable)) { $extension->updateVersionableFields($suffix, $fields, $indexes); } $extension->clearOwner(); } // Build _Live table if ($this->hasStages()) { $liveTable = $this->stageTable($suffixTable, static::LIVE); DB::require_table($liveTable, $fields, $indexes, false, $options); } // Build _versions table //Unique indexes will not work on versioned tables, so we'll convert them to standard indexes: $nonUniqueIndexes = $this->uniqueToIndex($indexes); if ($isRootClass) { // Create table for all versions $versionFields = array_merge(Config::inst()->get('SilverStripe\\ORM\\Versioning\\Versioned', 'db_for_versions_table'), (array) $fields); $versionIndexes = array_merge(Config::inst()->get('SilverStripe\\ORM\\Versioning\\Versioned', 'indexes_for_versions_table'), (array) $nonUniqueIndexes); } else { // Create fields for any tables of subclasses $versionFields = array_merge(array("RecordID" => "Int", "Version" => "Int"), (array) $fields); $versionIndexes = array_merge(array('RecordID_Version' => array('type' => 'unique', 'value' => '"RecordID","Version"'), 'RecordID' => true, 'Version' => true), (array) $nonUniqueIndexes); } // Cleanup any orphans $this->cleanupVersionedOrphans("{$suffixBaseTable}_versions", "{$suffixTable}_versions"); // Build versions table DB::require_table("{$suffixTable}_versions", $versionFields, $versionIndexes, true, $options); } else { DB::dont_require_table("{$suffixTable}_versions"); if ($this->hasStages()) { $liveTable = $this->stageTable($suffixTable, static::LIVE); DB::dont_require_table($liveTable); } } } }