当前位置: 首页>>代码示例>>PHP>>正文


PHP Table::getForeignKeys方法代码示例

本文整理汇总了PHP中Phinx\Db\Table::getForeignKeys方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::getForeignKeys方法的具体用法?PHP Table::getForeignKeys怎么用?PHP Table::getForeignKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Phinx\Db\Table的用法示例。


在下文中一共展示了Table::getForeignKeys方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: createTable

 /**
  * {@inheritdoc}
  */
 public function createTable(Table $table)
 {
     $this->startCommandTimer();
     $options = $table->getOptions();
     // Add the default primary key
     $columns = $table->getPendingColumns();
     if (!isset($options['id']) || isset($options['id']) && $options['id'] === true) {
         $column = new Column();
         $column->setName('id')->setType('integer')->setIdentity(true);
         array_unshift($columns, $column);
         $options['primary_key'] = 'id';
     } elseif (isset($options['id']) && is_string($options['id'])) {
         // Handle id => "field_name" to support AUTO_INCREMENT
         $column = new Column();
         $column->setName($options['id'])->setType('integer')->setIdentity(true);
         array_unshift($columns, $column);
         $options['primary_key'] = $options['id'];
     }
     $sql = 'CREATE TABLE ';
     $sql .= $this->quoteTableName($table->getName()) . ' (';
     $sqlBuffer = array();
     $columnsWithComments = array();
     foreach ($columns as $column) {
         $sqlBuffer[] = $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column);
         // set column comments, if needed
         if ($column->getComment()) {
             $columnsWithComments[] = $column;
         }
     }
     // set the primary key(s)
     if (isset($options['primary_key'])) {
         $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', $table->getName());
         if (is_string($options['primary_key'])) {
             // handle primary_key => 'id'
             $pkSql .= $this->quoteColumnName($options['primary_key']);
         } elseif (is_array($options['primary_key'])) {
             // handle primary_key => array('tag_id', 'resource_id')
             // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the anonymous function,
             // but for now just hard-code the adapter quotes
             $pkSql .= implode(',', array_map(function ($v) {
                 return '[' . $v . ']';
             }, $options['primary_key']));
         }
         $pkSql .= ')';
         $sqlBuffer[] = $pkSql;
     }
     // set the foreign keys
     $foreignKeys = $table->getForeignKeys();
     if (!empty($foreignKeys)) {
         foreach ($foreignKeys as $foreignKey) {
             $sqlBuffer[] = $this->getForeignKeySqlDefinition($foreignKey, $table->getName());
         }
     }
     $sql .= implode(', ', $sqlBuffer);
     $sql .= ');';
     // process column comments
     if (!empty($columnsWithComments)) {
         foreach ($columnsWithComments as $column) {
             $sql .= $this->getColumnCommentSqlDefinition($column, $table->getName());
         }
     }
     // set the indexes
     $indexes = $table->getIndexes();
     if (!empty($indexes)) {
         foreach ($indexes as $index) {
             $sql .= $this->getIndexSqlDefinition($index, $table->getName());
         }
     }
     // execute the sql
     $this->writeCommand('createTable', array($table->getName()));
     $this->execute($sql);
     $this->endCommandTimer();
 }
开发者ID:lhas,项目名称:pep,代码行数:76,代码来源:SqlServerAdapter.php

示例2: createTable

 /**
  * {@inheritdoc}
  */
 public function createTable(Table $table)
 {
     $this->startCommandTimer();
     // Add the default primary key
     $columns = $table->getPendingColumns();
     $options = $table->getOptions();
     if (!isset($options['id']) || isset($options['id']) && $options['id'] === true) {
         $column = new Column();
         $column->setName('id')->setType('integer')->setIdentity(true);
         array_unshift($columns, $column);
     } elseif (isset($options['id']) && is_string($options['id'])) {
         // Handle id => "field_name" to support AUTO_INCREMENT
         $column = new Column();
         $column->setName($options['id'])->setType('integer')->setIdentity(true);
         array_unshift($columns, $column);
     }
     $sql = 'CREATE TABLE ';
     $sql .= $this->quoteTableName($table->getName()) . ' (';
     foreach ($columns as $column) {
         $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', ';
     }
     // set the primary key(s)
     if (isset($options['primary_key'])) {
         $sql = rtrim($sql);
         $sql .= ' PRIMARY KEY (';
         if (is_string($options['primary_key'])) {
             // handle primary_key => 'id'
             $sql .= $this->quoteColumnName($options['primary_key']);
         } elseif (is_array($options['primary_key'])) {
             // handle primary_key => array('tag_id', 'resource_id')
             // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the anonymous function,
             // but for now just hard-code the adapter quotes
             $sql .= implode(',', array_map(function ($v) {
                 return '`' . $v . '`';
             }, $options['primary_key']));
         }
         $sql .= ')';
     } else {
         $sql = substr(rtrim($sql), 0, -1);
         // no primary keys
     }
     // set the foreign keys
     $foreignKeys = $table->getForeignKeys();
     if (!empty($foreignKeys)) {
         foreach ($foreignKeys as $foreignKey) {
             $sql .= ', ' . $this->getForeignKeySqlDefinition($foreignKey);
         }
     }
     $sql = rtrim($sql) . ');';
     // execute the sql
     $this->writeCommand('createTable', array($table->getName()));
     $this->execute($sql);
     $this->endCommandTimer();
     foreach ($table->getIndexes() as $index) {
         $this->addIndex($table, $index);
     }
 }
开发者ID:elliotwms,项目名称:phinx,代码行数:60,代码来源:SQLiteAdapter.php

示例3: createTable

 /**
  * {@inheritdoc}
  */
 public function createTable(Table $table)
 {
     $this->startCommandTimer();
     // This method is based on the MySQL docs here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html
     $defaultOptions = array('engine' => 'InnoDB', 'collation' => 'utf8_general_ci');
     $options = array_merge($defaultOptions, $table->getOptions());
     // Add the default primary key
     $columns = $table->getPendingColumns();
     if (!isset($options['id']) || isset($options['id']) && $options['id'] === true) {
         $column = new Column();
         $column->setName('id')->setType('integer')->setIdentity(true);
         array_unshift($columns, $column);
         $options['primary_key'] = 'id';
     } elseif (isset($options['id']) && is_string($options['id'])) {
         // Handle id => "field_name" to support AUTO_INCREMENT
         $column = new Column();
         $column->setName($options['id'])->setType('integer')->setIdentity(true);
         array_unshift($columns, $column);
         $options['primary_key'] = $options['id'];
     }
     // TODO - process table options like collation etc
     // process table engine (default to InnoDB)
     $optionsStr = 'ENGINE = InnoDB';
     if (isset($options['engine'])) {
         $optionsStr = sprintf('ENGINE = %s', $options['engine']);
     }
     // process table collation
     if (isset($options['collation'])) {
         $charset = explode('_', $options['collation']);
         $optionsStr .= sprintf(' CHARACTER SET %s', $charset[0]);
         $optionsStr .= sprintf(' COLLATE %s', $options['collation']);
     }
     // set the table comment
     if (isset($options['comment'])) {
         $optionsStr .= sprintf(" COMMENT=%s ", $this->getConnection()->quote($options['comment']));
     }
     $sql = 'CREATE TABLE ';
     $sql .= $this->quoteTableName($table->getName()) . ' (';
     foreach ($columns as $column) {
         $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', ';
     }
     // set the primary key(s)
     if (isset($options['primary_key'])) {
         $sql = rtrim($sql);
         $sql .= ' PRIMARY KEY (';
         if (is_string($options['primary_key'])) {
             // handle primary_key => 'id'
             $sql .= $this->quoteColumnName($options['primary_key']);
         } elseif (is_array($options['primary_key'])) {
             // handle primary_key => array('tag_id', 'resource_id')
             // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the
             // anonymous function, but for now just hard-code the adapter quotes
             $sql .= implode(',', array_map(function ($v) {
                 return '`' . $v . '`';
             }, $options['primary_key']));
         }
         $sql .= ')';
     } else {
         $sql = substr(rtrim($sql), 0, -1);
         // no primary keys
     }
     // set the indexes
     $indexes = $table->getIndexes();
     if (!empty($indexes)) {
         foreach ($indexes as $index) {
             $sql .= ', ' . $this->getIndexSqlDefinition($index);
         }
     }
     // set the foreign keys
     $foreignKeys = $table->getForeignKeys();
     if (!empty($foreignKeys)) {
         foreach ($foreignKeys as $foreignKey) {
             $sql .= ', ' . $this->getForeignKeySqlDefinition($foreignKey);
         }
     }
     $sql .= ') ' . $optionsStr;
     $sql = rtrim($sql) . ';';
     // execute the sql
     $this->writeCommand('createTable', array($table->getName()));
     $this->execute($sql);
     $this->endCommandTimer();
 }
开发者ID:parkerj,项目名称:eduTrac-SIS,代码行数:85,代码来源:MysqlAdapter.php

示例4: testAddTableWithForeignKey

 public function testAddTableWithForeignKey()
 {
     $this->mock->expects($this->any())->method('isValidColumnType')->with($this->callback(function ($column) {
         return in_array($column->getType(), array('string', 'integer'));
     }))->will($this->returnValue(true));
     $table = new Table('table', array(), $this->adapter);
     $table->addColumn('bar', 'string')->addColumn('relation', 'integer')->addForeignKey('relation', 'target_table', array('id'));
     $this->mock->expects($this->once())->method('createTable')->with($this->callback(function ($table) {
         if ($table->getName() !== 'pre_table_suf') {
             throw new \Exception(sprintf('Table::getName was not prefixed/suffixed properly: "%s"', $table->getName()));
         }
         $fks = $table->getForeignKeys();
         if (count($fks) !== 1) {
             throw new \Exception(sprintf('Table::getForeignKeys count was incorrect: %d', count($fks)));
         }
         foreach ($fks as $fk) {
             if ($fk->getReferencedTable()->getName() !== 'pre_target_table_suf') {
                 throw new \Exception(sprintf('ForeignKey::getReferencedTable was not prefixed/suffixed properly: "%s"', $fk->getReferencedTable->getName()));
             }
         }
         return true;
     }));
     $table->create();
 }
开发者ID:stephenorr,项目名称:phinx,代码行数:24,代码来源:TablePrefixAdapterTest.php


注:本文中的Phinx\Db\Table::getForeignKeys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。