protected function createTaggingTable() { $table = $this->getTable(); $database = $table->getDatabase(); $pks = $this->getTable()->getPrimaryKey(); if (count($pks) > 1) { throw new EngineException('The Taggable behavior does not support tables with composite primary keys'); } $taggingTableName = $this->getTaggingTableName(); if ($database->hasTable($taggingTableName)) { $this->taggingTable = $database->getTable($taggingTableName); } else { $this->taggingTable = $database->addTable(array('name' => $taggingTableName, 'phpName' => $this->replaceTokens($this->getParameter('tagging_table_phpname')), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => '\\' . $table->getNamespace())); // every behavior adding a table should re-execute database behaviors // see bug 2188 http://www.propelorm.org/changeset/2188 foreach ($database->getBehaviors() as $behavior) { $behavior->modifyDatabase(); } } if ($this->taggingTable->hasColumn('tag_id')) { $tagFkColumn = $this->taggingTable->getColumn('tag_id'); } else { $tagFkColumn = $this->taggingTable->addColumn(array('name' => 'tag_id', 'type' => PropelTypes::INTEGER, 'primaryKey' => 'true')); } if ($this->taggingTable->hasColumn($table->getName() . '_id')) { $objFkColumn = $this->taggingTable->getColumn($table->getName() . '_id'); } else { $objFkColumn = $this->taggingTable->addColumn(array('name' => $table->getName() . '_id', 'type' => PropelTypes::INTEGER, 'primaryKey' => 'true')); } $this->taggingTable->setIsCrossRef(true); $fkTag = new ForeignKey(); $fkTag->setForeignTableCommonName($this->tagTable->getCommonName()); $fkTag->setForeignSchemaName($this->tagTable->getSchema()); $fkTag->setOnDelete(ForeignKey::CASCADE); $fkTag->setOnUpdate(ForeignKey::CASCADE); $tagColumn = $this->tagTable->getColumn('id'); $fkTag->addReference($tagFkColumn->getName(), $tagColumn->getName()); $this->taggingTable->addForeignKey($fkTag); $fkObj = new ForeignKey(); $fkObj->setForeignTableCommonName($this->getTable()->getCommonName()); $fkObj->setForeignSchemaName($this->getTable()->getSchema()); $fkObj->setOnDelete(ForeignKey::CASCADE); $fkObj->setOnUpdate(ForeignKey::CASCADE); foreach ($pks as $column) { $fkObj->addReference($objFkColumn->getName(), $column->getName()); } $this->taggingTable->addForeignKey($fkObj); }
protected function doNaming() { if (!$this->name || $this->autoNaming) { $newName = sprintf('%s_', $this instanceof Unique ? 'u' : 'i'); if ($this->columns) { $hash = []; $hash[] = implode(',', (array) $this->columns); $hash[] = implode(',', (array) $this->columnsSize); $newName .= substr(md5(strtolower(implode(':', $hash))), 0, 6); } else { $newName .= 'no_columns'; } if ($this->table) { $newName = $this->table->getCommonName() . '_' . $newName; } $this->name = $newName; $this->autoNaming = true; } }
public function testQualifiedName() { $table = new Table(); $table->setSchema("foo"); $table->setCommonName("bar"); $this->assertEquals($table->getName(), "bar"); $this->assertEquals($table->getCommonName(), "bar"); $database = new Database(); $database->addTable($table); $database->setPlatform(new NoSchemaPlatform()); $this->assertEquals($table->getName(), "bar"); $database->setPlatform(new SchemaPlatform()); $this->assertEquals($table->getName(), "foo.bar"); }
/** * Returns the name of a table primary key * @return string */ public function getPrimaryKeyName(Table $table) { $tableName = $table->getCommonName(); return $tableName . '_PK'; }