本文整理汇总了PHP中Propel\Generator\Model\ForeignKey类的典型用法代码示例。如果您正苦于以下问题:PHP ForeignKey类的具体用法?PHP ForeignKey怎么用?PHP ForeignKey使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ForeignKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: relateI18nTableToMainTable
protected function relateI18nTableToMainTable()
{
$table = $this->getTable();
$i18nTable = $this->i18nTable;
$pks = $this->getTable()->getPrimaryKey();
if (count($pks) > 1) {
throw new EngineException('The i18n behavior does not support tables with composite primary keys');
}
foreach ($pks as $column) {
if (!$i18nTable->hasColumn($column->getName())) {
$column = clone $column;
$column->setAutoIncrement(false);
$i18nTable->addColumn($column);
}
}
if (in_array($table->getName(), $i18nTable->getForeignTableNames())) {
return;
}
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setDefaultJoin('LEFT JOIN');
$fk->setOnDelete(ForeignKey::CASCADE);
$fk->setOnUpdate(ForeignKey::NONE);
foreach ($pks as $column) {
$fk->addReference($column->getName(), $column->getName());
}
$i18nTable->addForeignKey($fk);
}
示例2: addClosureColumn
protected function addClosureColumn($name, Table $ct_table, Column $column)
{
$table = $this->getTable();
$id_fieldname = $column->getName();
$domain = $column->getDomain();
if (!$ct_table->hasColumn($name)) {
$column = new Column($name);
$column->setDomain($domain);
$column->setPrimaryKey(true);
$ct_table->addColumn($column);
} else {
$column = $ct_table->getColumn($name);
}
$ct_tablename_normalized = str_replace('_', '', $ct_table->getName());
$fk_name = $ct_tablename_normalized . '_' . $name . '_fk';
if (!$ct_table->getColumnForeignKeys($name)) {
$column_fk = new ForeignKey($fk_name);
$column_fk->addReference($name, $table->getColumn($id_fieldname)->getName());
$column_fk->setForeignTableCommonName($table->getName());
$column_fk->setOnUpdate('cascade');
$column_fk->setOnDelete('restrict');
$ct_table->addForeignKey($column_fk);
}
$column_idx_name = $fk_name . '_idx';
if (!$ct_table->hasIndex($column_idx_name)) {
$column_idx = new Index($column_idx_name);
$column_idx->addColumn(['name' => $column->getName()]);
$ct_table->addIndex($column_idx);
}
}
示例3: modifyTable
/**
* Adds all columns, indexes, constraints and additional tables.
*/
public function modifyTable()
{
$table = $this->getTable();
$tableName = $table->getName();
$foreignTableName = $this->getForeignTable();
// enable reload on insert to force the model to load the trigger generated id(s)
$table->setReloadOnInsert(true);
$foreignIdColumnName = $foreignTableName . '_id';
$compositeKeyColumnName = $foreignTableName . '_' . $tableName . '_id';
if ($table->hasBehavior('concrete_inheritance')) {
// we're a child in a concrete inheritance
$parentTableName = $table->getBehavior('concrete_inheritance')->getParameter('extends');
$parentTable = $table->getDatabase()->getTable($parentTableName);
if ($parentTable->hasBehavior('\\' . __CLASS__)) {
//we're a child of a concrete inheritance structure, so we're going to skip this
//round here because this behavior has also been attached by the parent table.
return;
}
}
if ($table->hasColumn($foreignIdColumnName)) {
$foreignIdColumn = $table->getColumn($foreignIdColumnName);
} else {
$foreignIdColumn = $table->addColumn(array('name' => $foreignIdColumnName, 'type' => 'integer', 'required' => true));
$compositeKeyForeignKeyName = $tableName . '_FK_' . $foreignIdColumnName;
$foreignKey = new ForeignKey($compositeKeyForeignKeyName);
$foreignKey->addReference($foreignIdColumnName, 'id');
$foreignKey->setForeignTableCommonName($foreignTableName);
$foreignKey->setOnUpdate(ForeignKey::CASCADE);
$foreignKey->setOnDelete(ForeignKey::CASCADE);
$table->addForeignKey($foreignKey);
}
if ($table->hasColumn($compositeKeyColumnName)) {
$compositeKeyColumn = $table->getColumn($compositeKeyColumnName);
} else {
$compositeKeyColumn = $table->addColumn(array('name' => $compositeKeyColumnName, 'type' => 'integer', 'required' => false));
}
$index = new Unique($tableName . '_UQ_' . $foreignIdColumnName . '_' . $compositeKeyColumnName);
$index->addColumn($foreignIdColumn);
$index->addColumn($compositeKeyColumn);
$table->addUnique($index);
$database = $table->getDatabase();
$sequenceTableName = sprintf('%s_sequence', $foreignTableName);
if (!$database->hasTable($sequenceTableName)) {
$sequenceTable = $database->addTable(array('name' => $sequenceTableName, 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null, 'skipSql' => $table->isSkipSql()));
$sequenceTable->addColumn(array('name' => 'table_name', 'type' => 'varchar', 'size' => 32, 'required' => true, 'primaryKey' => true));
$sequenceTable->addColumn(array('name' => $foreignIdColumnName, 'type' => 'integer', 'required' => true, 'primaryKey' => true));
$sequenceTable->addColumn(array('name' => $foreignTableName . '_max_sequence_id', 'type' => 'integer', 'required' => false, 'default' => null));
}
}
示例4: relateDelegateToMainTable
protected function relateDelegateToMainTable($delegateTable, $mainTable)
{
$pks = $mainTable->getPrimaryKey();
foreach ($pks as $column) {
$mainColumnName = $column->getName();
if (!$delegateTable->hasColumn($mainColumnName)) {
$column = clone $column;
$column->setAutoIncrement(false);
$delegateTable->addColumn($column);
}
}
// Add a one-to-one fk
$fk = new ForeignKey();
$fk->setForeignTableCommonName($mainTable->getCommonName());
$fk->setForeignSchemaName($mainTable->getSchema());
$fk->setDefaultJoin('LEFT JOIN');
$fk->setOnDelete(ForeignKey::CASCADE);
$fk->setOnUpdate(ForeignKey::NONE);
foreach ($pks as $column) {
$fk->addReference($column->getName(), $column->getName());
}
$delegateTable->addForeignKey($fk);
}
示例5: getForeignKeyDDL
public function getForeignKeyDDL(ForeignKey $fk)
{
if ($fk->isSkipSql() || $fk->isPolymorphic()) {
return;
}
$pattern = 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)';
$script = sprintf($pattern, $this->quoteIdentifier($fk->getName()), $this->getColumnListDDL($fk->getLocalColumnObjects()), $this->quoteIdentifier($fk->getForeignTableName()), $this->getColumnListDDL($fk->getForeignColumnObjects()));
if ($fk->hasOnUpdate() && $fk->getOnUpdate() != ForeignKey::SETNULL) {
$script .= ' ON UPDATE ' . $fk->getOnUpdate();
}
if ($fk->hasOnDelete() && $fk->getOnDelete() != ForeignKey::SETNULL) {
$script .= ' ON DELETE ' . $fk->getOnDelete();
}
return $script;
}
示例6: getRefRelatedBySuffix
protected static function getRefRelatedBySuffix(ForeignKey $fk)
{
$relCol = '';
foreach ($fk->getLocalForeignMapping() as $localColumnName => $foreignColumnName) {
$localTable = $fk->getTable();
$localColumn = $localTable->getColumn($localColumnName);
if (!$localColumn) {
throw new RuntimeException(sprintf('Could not fetch column: %s in table %s.', $localColumnName, $localTable->getName()));
}
$foreignKeysToForeignTable = $localTable->getForeignKeysReferencingTable($fk->getForeignTableName());
if ($fk->getForeignTableName() == $fk->getTableName()) {
// self referential foreign key
$relCol .= $fk->getForeignTable()->getColumn($foreignColumnName)->getPhpName();
if (count($foreignKeysToForeignTable) > 1) {
// several self-referential foreign keys
$relCol .= array_search($fk, $foreignKeysToForeignTable);
}
} elseif (count($foreignKeysToForeignTable) > 1 || count($fk->getForeignTable()->getForeignKeysReferencingTable($fk->getTableName())) > 0) {
// several foreign keys to the same table, or symmetrical foreign key in foreign table
$relCol .= $localColumn->getPhpName();
}
}
if (!empty($relCol)) {
$relCol = 'RelatedBy' . $relCol;
}
return $relCol;
}
示例7: getForeignKeyDDL
public function getForeignKeyDDL(ForeignKey $fk)
{
if ($fk->isSkipSql() || !$this->foreignKeySupport) {
return;
}
$pattern = "FOREIGN KEY (%s) REFERENCES %s (%s)";
$script = sprintf($pattern, $this->getColumnListDDL($fk->getLocalColumns()), $this->quoteIdentifier($fk->getForeignTableName()), $this->getColumnListDDL($fk->getForeignColumns()));
if ($fk->hasOnUpdate()) {
$script .= "\n ON UPDATE " . $fk->getOnUpdate();
}
if ($fk->hasOnDelete()) {
$script .= "\n ON DELETE " . $fk->getOnDelete();
}
return $script;
}
示例8: addForeignKeys
protected function addForeignKeys(Table $table)
{
$database = $table->getDatabase();
$stmt = $this->dbh->query('PRAGMA foreign_key_list("' . $table->getName() . '")');
$lastId = null;
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
if ($lastId !== $row['id']) {
$fk = new ForeignKey();
$onDelete = $row['on_delete'];
if ($onDelete && 'NO ACTION' !== $onDelete) {
$fk->setOnDelete($onDelete);
}
$onUpdate = $row['on_update'];
if ($onUpdate && 'NO ACTION' !== $onUpdate) {
$fk->setOnUpdate($onUpdate);
}
$foreignTable = $database->getTable($row['table'], true);
if (!$foreignTable) {
continue;
}
$table->addForeignKey($fk);
$fk->setForeignTableCommonName($foreignTable->getCommonName());
if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) {
$fk->setForeignSchemaName($foreignTable->guessSchemaName());
}
$lastId = $row['id'];
}
$fk->addReference($row['from'], $row['to']);
}
}
示例9: addRefFkScheduledForDeletionAttribute
protected function addRefFkScheduledForDeletionAttribute(&$script, ForeignKey $refFK)
{
$className = $this->getClassNameFromTable($refFK->getTable());
$fkName = lcfirst($this->getRefFKPhpNameAffix($refFK, true));
$script .= "\n /**\n * An array of objects scheduled for deletion.\n * @var ObjectCollection|{$className}[]\n */\n protected \${$fkName}ScheduledForDeletion = null;\n";
}
示例10: modifyTable
public function modifyTable()
{
$table = $this->getTable();
$parentTable = $this->getParentTable();
if ($this->isCopyData()) {
// tell the parent table that it has a descendant
if (!$parentTable->hasBehavior('concrete_inheritance_parent')) {
$parentBehavior = new ConcreteInheritanceParentBehavior();
$parentBehavior->setName('concrete_inheritance_parent');
$parentBehavior->addParameter(array('name' => 'descendant_column', 'value' => $this->getParameter('descendant_column')));
$parentTable->addBehavior($parentBehavior);
// The parent table's behavior modifyTable() must be executed before this one
$parentBehavior->getTableModifier()->modifyTable();
$parentBehavior->setTableModified(true);
}
}
// Add the columns of the parent table
foreach ($parentTable->getColumns() as $column) {
if ($column->getName() == $this->getParameter('descendant_column')) {
continue;
}
if ($table->hasColumn($column->getName())) {
continue;
}
$copiedColumn = clone $column;
if ($column->isAutoIncrement() && $this->isCopyData()) {
$copiedColumn->setAutoIncrement(false);
}
$table->addColumn($copiedColumn);
if ($column->isPrimaryKey() && $this->isCopyData()) {
$fk = new ForeignKey();
$fk->setForeignTableCommonName($column->getTable()->getCommonName());
$fk->setForeignSchemaName($column->getTable()->getSchema());
$fk->setOnDelete('CASCADE');
$fk->setOnUpdate(null);
$fk->addReference($copiedColumn, $column);
$fk->isParentChild = true;
$table->addForeignKey($fk);
}
}
// add the foreign keys of the parent table
foreach ($parentTable->getForeignKeys() as $fk) {
$copiedFk = clone $fk;
$copiedFk->setName('');
$copiedFk->setRefPhpName('');
$this->getTable()->addForeignKey($copiedFk);
}
// add the indices of the parent table
foreach ($parentTable->getIndices() as $index) {
$copiedIndex = clone $index;
$copiedIndex->setName('');
$this->getTable()->addIndex($copiedIndex);
}
// add the unique indices of the parent table
foreach ($parentTable->getUnices() as $unique) {
$copiedUnique = clone $unique;
$copiedUnique->setName('');
$this->getTable()->addUnique($copiedUnique);
}
// add the Behaviors of the parent table
foreach ($parentTable->getBehaviors() as $behavior) {
if ($behavior->getName() == 'concrete_inheritance_parent' || $behavior->getName() == 'concrete_inheritance') {
continue;
}
//validate behavior. If validate behavior already exists, clone only rules from parent
if ('validate' === $behavior->getName() && $table->hasBehavior('validate')) {
$table->getBehavior('validate')->mergeParameters($behavior->getParameters());
continue;
}
$copiedBehavior = clone $behavior;
$copiedBehavior->setTableModified(false);
$this->getTable()->addBehavior($copiedBehavior);
}
}
示例11: addCrossFKDoAdd
/**
* @param string &$script The script will be modified in this method.
* @param ForeignKey $refFK
* @param ForeignKey $crossFK
*/
protected function addCrossFKDoAdd(&$script, ForeignKey $refFK, ForeignKey $crossFK)
{
$relatedObjectClassName = $this->getFKPhpNameAffix($crossFK, $plural = false);
// lcfirst() doesn't exist in PHP < 5.3
$lowerRelatedObjectClassName = $relatedObjectClassName;
$lowerRelatedObjectClassName[0] = strtolower($lowerRelatedObjectClassName[0]);
$joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
$className = $joinedTableObjectBuilder->getObjectClassname();
$tblFK = $refFK->getTable();
$foreignObjectName = '$' . $tblFK->getStudlyPhpName();
$script .= "\n /**\n * @param {$relatedObjectClassName} \${$lowerRelatedObjectClassName} The {$lowerRelatedObjectClassName} object to add.\n */\n protected function doAdd{$relatedObjectClassName}(\${$lowerRelatedObjectClassName})\n {\n {$foreignObjectName} = new {$className}();\n {$foreignObjectName}->set{$relatedObjectClassName}(\${$lowerRelatedObjectClassName});\n \$this->add{$className}({$foreignObjectName});\n }\n";
}
示例12: appendForeignKeyNode
/**
* Appends the generated <foreign-key> XML node to its parent node.
*
* @param ForeignKey $foreignKey The ForeignKey model instance
* @param \DOMNode $parentNode The parent DOMNode object
*/
private function appendForeignKeyNode(ForeignKey $foreignKey, \DOMNode $parentNode)
{
$foreignKeyNode = $parentNode->appendChild($this->document->createElement('foreign-key'));
$foreignKeyNode->setAttribute('foreignTable', $foreignKey->getForeignTableCommonName());
if ($schema = $foreignKey->getForeignSchemaName()) {
$foreignKeyNode->setAttribute('foreignSchema', $schema);
}
$foreignKeyNode->setAttribute('name', $foreignKey->getName());
if ($phpName = $foreignKey->getPhpName()) {
$foreignKeyNode->setAttribute('phpName', $phpName);
}
if ($refPhpName = $foreignKey->getRefPhpName()) {
$foreignKeyNode->setAttribute('refPhpName', $refPhpName);
}
if ($defaultJoin = $foreignKey->getDefaultJoin()) {
$foreignKeyNode->setAttribute('defaultJoin', $defaultJoin);
}
if ($onDeleteBehavior = $foreignKey->getOnDelete()) {
$foreignKeyNode->setAttribute('onDelete', $onDeleteBehavior);
}
if ($onUpdateBehavior = $foreignKey->getOnUpdate()) {
$foreignKeyNode->setAttribute('onUpdate', $onUpdateBehavior);
}
for ($i = 0, $size = count($foreignKey->getLocalColumns()); $i < $size; $i++) {
$refNode = $foreignKeyNode->appendChild($this->document->createElement('reference'));
$refNode->setAttribute('local', $foreignKey->getLocalColumnName($i));
$refNode->setAttribute('foreign', $foreignKey->getForeignColumnName($i));
}
foreach ($foreignKey->getVendorInformation() as $vendorInformation) {
$this->appendVendorInformationNode($vendorInformation, $foreignKeyNode);
}
}
示例13: addSnapshotTable
private function addSnapshotTable()
{
$table = $this->getTable();
$primaryKeyColumn = $table->getFirstPrimaryKeyColumn();
$database = $table->getDatabase();
$snapshotTableName = $this->getParameter(self::PARAMETER_SNAPSHOT_TABLE) ?: $this->getDefaultSnapshotTableName();
if ($database->hasTable($snapshotTableName)) {
$this->snapshotTable = $database->getTable($snapshotTableName);
return;
}
$snapshotTable = $database->addTable(['name' => $snapshotTableName, 'phpName' => $this->getParameter(self::PARAMETER_SNAPSHOT_PHPNAME), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null]);
$addSnapshotAt = true;
$hasTimestampableBehavior = 0 < count(array_filter($database->getBehaviors(), function (Behavior $behavior) {
return 'timestampable' === $behavior->getName();
}));
if ($hasTimestampableBehavior) {
$addSnapshotAt = false;
$timestampableBehavior = clone $database->getBehavior('timestampable');
$timestampableBehavior->setParameters(array_merge($timestampableBehavior->getParameters(), ['create_column' => $this->getParameter(self::PARAMETER_SNAPSHOT_AT_COLUMN), 'disable_updated_at' => 'true']));
$snapshotTable->addBehavior($timestampableBehavior);
}
$snapshotTable->isSnapshotTable = true;
$idColumn = $snapshotTable->addColumn(['name' => 'id', 'type' => 'INTEGER']);
$idColumn->setAutoIncrement(true);
$idColumn->setPrimaryKey(true);
$idColumn->setNotNull(true);
$columns = $table->getColumns();
foreach ($columns as $column) {
if ($column->isPrimaryKey()) {
continue;
}
$columnInSnapshotTable = clone $column;
$columnInSnapshotTable->setNotNull(false);
if ($columnInSnapshotTable->hasReferrers()) {
$columnInSnapshotTable->clearReferrers();
}
if ($columnInSnapshotTable->isAutoincrement()) {
$columnInSnapshotTable->setAutoIncrement(false);
}
$snapshotTable->addColumn($columnInSnapshotTable);
}
$foreignKeyColumn = $snapshotTable->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN), 'type' => $primaryKeyColumn->getType(), 'size' => $primaryKeyColumn->getSize()]);
$index = new Index();
$index->setName($this->getParameter(self::PARAMETER_REFERENCE_COLUMN));
if ($primaryKeyColumn->getSize()) {
$index->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN), 'size' => $primaryKeyColumn->getSize()]);
} else {
$index->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN)]);
}
$snapshotTable->addIndex($index);
$foreignKey = new ForeignKey();
$foreignKey->setName(vsprintf('fk_%s_%s', [$snapshotTable->getOriginCommonName(), $this->getParameter(self::PARAMETER_REFERENCE_COLUMN)]));
$foreignKey->setOnUpdate('CASCADE');
$foreignKey->setOnDelete('SET NULL');
$foreignKey->setForeignTableCommonName($this->getTable()->getCommonName());
$foreignKey->addReference($foreignKeyColumn, $primaryKeyColumn);
$snapshotTable->addForeignKey($foreignKey);
if ($this->getParameter(self::PARAMETER_LOG_SNAPSHOT_AT) == 'true' && $addSnapshotAt) {
$snapshotTable->addColumn(['name' => $this->getParameter(self::PARAMETER_SNAPSHOT_AT_COLUMN), 'type' => 'TIMESTAMP']);
}
$indices = $table->getIndices();
foreach ($indices as $index) {
$copiedIndex = clone $index;
$snapshotTable->addIndex($copiedIndex);
}
// copy unique indices to indices
// see https://github.com/propelorm/Propel/issues/175 for details
$unices = $table->getUnices();
foreach ($unices as $unique) {
$index = new Index();
$index->setName($unique->getName());
$columns = $unique->getColumns();
foreach ($columns as $columnName) {
if ($size = $unique->getColumnSize($columnName)) {
$index->addColumn(['name' => $columnName, 'size' => $size]);
} else {
$index->addColumn(['name' => $columnName]);
}
}
$snapshotTable->addIndex($index);
}
$behaviors = $database->getBehaviors();
foreach ($behaviors as $behavior) {
$behavior->modifyDatabase();
}
$this->snapshotTable = $snapshotTable;
}
示例14: providerForTestGetForeignKeysDDL
public function providerForTestGetForeignKeysDDL()
{
$db = new Database();
$db->setIdentifierQuoting(true);
$table1 = new Table('foo');
$db->addTable($table1);
$column1 = new Column('bar_id');
$column1->getDomain()->copy(new Domain('FOOTYPE'));
$table1->addColumn($column1);
$table2 = new Table('bar');
$db->addTable($table2);
$column2 = new Column('id');
$column2->getDomain()->copy(new Domain('BARTYPE'));
$table2->addColumn($column2);
$fk = new ForeignKey('foo_bar_fk');
$fk->setForeignTableCommonName('bar');
$fk->addReference($column1, $column2);
$fk->setOnDelete('CASCADE');
$table1->addForeignKey($fk);
$column3 = new Column('baz_id');
$column3->getDomain()->copy(new Domain('BAZTYPE'));
$table1->addColumn($column3);
$table3 = new Table('baz');
$db->addTable($table3);
$column4 = new Column('id');
$column4->getDomain()->copy(new Domain('BAZTYPE'));
$table3->addColumn($column4);
$fk = new ForeignKey('foo_baz_fk');
$fk->setForeignTableCommonName('baz');
$fk->addReference($column3, $column4);
$fk->setOnDelete('SETNULL');
$table1->addForeignKey($fk);
return array(array($table1));
}
示例15: addForeignKey
/**
* Adds a relation from logTable to origin table.
*
* @param Table $logTable
*/
protected function addForeignKey(Table $logTable)
{
$table = $this->getTable();
if ($table->getForeignKeysReferencingTable($table->getName())) {
//if already a foreignKey exist to origin table then don't add a second.
return;
}
// create the foreign key
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setPhpName('Origin');
$fk->setOnDelete('CASCADE');
$fk->setOnUpdate('CASCADE');
foreach ($table->getPrimaryKey() as $column) {
$fk->addReference($logTable->getColumn($column->getName()), $column);
}
$logTable->addForeignKey($fk);
}