本文整理汇总了PHP中SQLSelect::create方法的典型用法代码示例。如果您正苦于以下问题:PHP SQLSelect::create方法的具体用法?PHP SQLSelect::create怎么用?PHP SQLSelect::create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SQLSelect
的用法示例。
在下文中一共展示了SQLSelect::create方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: migrateAuthors
/**
* For each author, add an AuthorHelper
*/
private function migrateAuthors()
{
/** @var SQLSelect $query */
$query = SQLSelect::create();
$query->setSelect('Author')->setFrom('News')->setDistinct(true);
$authors = $query->execute();
foreach ($authors as $author) {
/** Create a new author if it doesn't exist */
if (!($authorHelper = AuthorHelper::get()->filter(array('OriginalName' => trim($author['Author'])))->first())) {
/** @var AuthorHelper $authorHelper */
$authorHelper = AuthorHelper::create();
$authorHelper->OriginalName = $author['Author'];
$authorHelper->write();
}
$sql = "UPDATE `News` SET `AuthorHelperID` = '" . $authorHelper->ID . "' WHERE Author = '" . $author['Author'] . "'";
DB::query($sql);
}
}
示例2: augmentDatabase
public function augmentDatabase()
{
$classTable = $this->owner->class;
$isRootClass = $this->owner->class == ClassInfo::baseDataClass($this->owner->class);
// Build a list of suffixes whose tables need versioning
$allSuffixes = array();
foreach (Versioned::$versionableExtensions as $versionableExtension => $suffixes) {
if ($this->owner->hasExtension($versionableExtension)) {
$allSuffixes = array_merge($allSuffixes, (array) $suffixes);
foreach ((array) $suffixes as $suffix) {
$allSuffixes[$suffix] = $versionableExtension;
}
}
}
// Add the default table with an empty suffix to the list (table name = class name)
array_push($allSuffixes, '');
foreach ($allSuffixes as $key => $suffix) {
// check that this is a valid suffix
if (!is_int($key)) {
continue;
}
if ($suffix) {
$table = "{$classTable}_{$suffix}";
} else {
$table = $classTable;
}
$fields = DataObject::database_fields($this->owner->class);
unset($fields['ID']);
if ($fields) {
$options = Config::inst()->get($this->owner->class, 'create_table_options', Config::FIRST_SET);
$indexes = $this->owner->databaseIndexes();
if ($suffix && ($ext = $this->owner->getExtensionInstance($allSuffixes[$suffix]))) {
if (!$ext->isVersionedTable($table)) {
continue;
}
$ext->setOwner($this->owner);
$fields = $ext->fieldsInExtraTables($suffix);
$ext->clearOwner();
$indexes = $fields['indexes'];
$fields = $fields['db'];
}
// Create tables for other stages
foreach ($this->stages as $stage) {
// Extra tables for _Live, etc.
// Change unique indexes to 'index'. Versioned tables may run into unique indexing difficulties
// otherwise.
$indexes = $this->uniqueToIndex($indexes);
if ($stage != $this->defaultStage) {
DB::require_table("{$table}_{$stage}", $fields, $indexes, false, $options);
}
// Version fields on each root table (including Stage)
/*
if($isRootClass) {
$stageTable = ($stage == $this->defaultStage) ? $table : "{$table}_$stage";
$parts=Array('datatype'=>'int', 'precision'=>11, 'null'=>'not null', 'default'=>(int)0);
$values=Array('type'=>'int', 'parts'=>$parts);
DB::requireField($stageTable, 'Version', $values);
}
*/
}
if ($isRootClass) {
// Create table for all versions
$versionFields = array_merge(Config::inst()->get('Versioned', 'db_for_versions_table'), (array) $fields);
$versionIndexes = array_merge(Config::inst()->get('Versioned', 'indexes_for_versions_table'), (array) $indexes);
} else {
// Create fields for any tables of subclasses
$versionFields = array_merge(array("RecordID" => "Int", "Version" => "Int"), (array) $fields);
//Unique indexes will not work on versioned tables, so we'll convert them to standard indexes:
$indexes = $this->uniqueToIndex($indexes);
$versionIndexes = array_merge(array('RecordID_Version' => array('type' => 'unique', 'value' => '"RecordID","Version"'), 'RecordID' => true, 'Version' => true), (array) $indexes);
}
if (DB::get_schema()->hasTable("{$table}_versions")) {
// Fix data that lacks the uniqueness constraint (since this was added later and
// bugs meant that the constraint was validated)
$duplications = DB::query("SELECT MIN(\"ID\") AS \"ID\", \"RecordID\", \"Version\"\n\t\t\t\t\t\tFROM \"{$table}_versions\" GROUP BY \"RecordID\", \"Version\"\n\t\t\t\t\t\tHAVING COUNT(*) > 1");
foreach ($duplications as $dup) {
DB::alteration_message("Removing {$table}_versions duplicate data for " . "{$dup['RecordID']}/{$dup['Version']}", "deleted");
DB::prepared_query("DELETE FROM \"{$table}_versions\" WHERE \"RecordID\" = ?\n\t\t\t\t\t\t\tAND \"Version\" = ? AND \"ID\" != ?", array($dup['RecordID'], $dup['Version'], $dup['ID']));
}
// Remove junk which has no data in parent classes. Only needs to run the following
// when versioned data is spread over multiple tables
if (!$isRootClass && ($versionedTables = ClassInfo::dataClassesFor($table))) {
foreach ($versionedTables as $child) {
if ($table === $child) {
break;
}
// only need subclasses
// Select all orphaned version records
$orphanedQuery = SQLSelect::create()->selectField("\"{$table}_versions\".\"ID\"")->setFrom("\"{$table}_versions\"");
// If we have a parent table limit orphaned records
// to only those that exist in this
if (DB::get_schema()->hasTable("{$child}_versions")) {
$orphanedQuery->addLeftJoin("{$child}_versions", "\"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\"\n\t\t\t\t\t\t\t\t\t\tAND \"{$child}_versions\".\"Version\" = \"{$table}_versions\".\"Version\"")->addWhere("\"{$child}_versions\".\"ID\" IS NULL");
}
$count = $orphanedQuery->count();
if ($count > 0) {
DB::alteration_message("Removing {$count} orphaned versioned records", "deleted");
$ids = $orphanedQuery->execute()->column();
foreach ($ids as $id) {
DB::prepared_query("DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?", array($id));
//.........这里部分代码省略.........