protected function validateTableColumns(Table $table) { if (!$table->hasPrimaryKey() && !$table->isSkipSql()) { $this->errors[] = sprintf('Table "%s" does not have a primary key defined. Propel requires all tables to have a primary key.', $table->getName()); } $phpNames = []; foreach ($table->getColumns() as $column) { if (in_array($column->getPhpName(), $phpNames)) { $this->errors[] = sprintf('Column "%s" declares a phpName already used in table "%s"', $column->getName(), $table->getName()); } $phpNames[] = $column->getPhpName(); } }
public function getAddTableDDL(Table $table) { $tableDescription = $table->hasDescription() ? $this->getCommentLineDDL($table->getDescription()) : ''; $lines = array(); foreach ($table->getColumns() as $column) { $lines[] = $this->getColumnDDL($column); } if ($table->hasPrimaryKey() && count($table->getPrimaryKey()) > 1) { $lines[] = $this->getPrimaryKeyDDL($table); } foreach ($table->getUnices() as $unique) { $lines[] = $this->getUniqueDDL($unique); } $sep = ",\n "; $pattern = "\n%sCREATE TABLE %s\n(\n %s\n);\n"; return sprintf($pattern, $tableDescription, $this->quoteIdentifier($table->getName()), implode($sep, $lines)); }
/** * Build the fields in the FormType. * * @param Table $table Table from which the fields will be extracted. * * @return string The FormType code. */ protected function buildFormFields(Table $table) { $buildCode = ''; foreach ($table->getColumns() as $column) { if ($column->isPrimaryKey()) { continue; } $name = $column->getPhpName(); // Use foreignKey table name, so the TypeGuesser gets it right if ($column->isForeignKey()) { /** @var ForeignKey $foreignKey */ $foreignKey = current($column->getForeignKeys()); $name = $foreignKey->getForeignTable()->getPhpName(); } $buildCode .= sprintf("\n \$builder->add('%s');", lcfirst($name)); } return $buildCode; }
protected function addUpdateLoadedNodes(&$script) { $queryClassName = $this->queryClassName; $objectClassName = $this->objectClassName; $tableMapClassName = $this->tableMapClassName; $script .= "\n/**\n * Reload all already loaded nodes to sync them with updated db\n *\n * @param {$objectClassName} \$prune Object to prune from the update\n * @param ConnectionInterface \$con Connection to use.\n */\nstatic public function updateLoadedNodes(\$prune = null, ConnectionInterface \$con = null)\n{\n if (Propel::isInstancePoolingEnabled()) {\n \$keys = array();\n /** @var \$obj {$objectClassName} */\n foreach ({$tableMapClassName}::\$instances as \$obj) {\n if (!\$prune || !\$prune->equals(\$obj)) {\n \$keys[] = \$obj->getPrimaryKey();\n }\n }\n\n if (!empty(\$keys)) {\n // We don't need to alter the object instance pool; we're just modifying these ones\n // already in the pool.\n \$criteria = new Criteria({$tableMapClassName}::DATABASE_NAME);"; if (1 === count($this->table->getPrimaryKey())) { $pkey = $this->table->getPrimaryKey(); $col = array_shift($pkey); $script .= "\n \$criteria->add(" . $this->builder->getColumnConstant($col) . ", \$keys, Criteria::IN);"; } else { $fields = array(); foreach ($this->table->getPrimaryKey() as $k => $col) { $fields[] = $this->builder->getColumnConstant($col); } $script .= "\n\n // Loop on each instances in pool\n foreach (\$keys as \$values) {\n // Create initial Criterion\n \$cton = \$criteria->getNewCriterion(" . $fields[0] . ", \$values[0]);"; unset($fields[0]); foreach ($fields as $k => $col) { $script .= "\n\n // Create next criterion\n \$nextcton = \$criteria->getNewCriterion(" . $col . ", \$values[{$k}]);\n // And merge it with the first\n \$cton->addAnd(\$nextcton);"; } $script .= "\n\n // Add final Criterion to Criteria\n \$criteria->addOr(\$cton);\n }"; } $script .= "\n \$dataFetcher = {$queryClassName}::create(null, \$criteria)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);\n while (\$row = \$dataFetcher->fetch()) {\n \$key = {$tableMapClassName}::getPrimaryKeyHashFromRow(\$row, 0);\n /** @var \$object {$objectClassName} */\n if (null !== (\$object = {$tableMapClassName}::getInstanceFromPool(\$key))) {"; $n = 0; foreach ($this->table->getColumns() as $col) { if ($col->isLazyLoad()) { continue; } if ($col->getPhpName() == $this->getColumnPhpName('left_column')) { $script .= "\n \$object->setLeftValue(\$row[{$n}]);"; } elseif ($col->getPhpName() == $this->getColumnPhpName('right_column')) { $script .= "\n \$object->setRightValue(\$row[{$n}]);"; } elseif ($this->getParameter('use_scope') == 'true' && $col->getPhpName() == $this->getColumnPhpName('scope_column')) { $script .= "\n \$object->setScopeValue(\$row[{$n}]);"; } elseif ($col->getPhpName() == $this->getColumnPhpName('level_column')) { $script .= "\n \$object->setLevel(\$row[{$n}]);\n \$object->clearNestedSetChildren();"; } $n++; } $script .= "\n }\n }\n \$dataFetcher->close();\n }\n }\n}\n"; }
public function getAddTableDDL(Table $table) { $lines = array(); foreach ($table->getColumns() as $column) { $lines[] = $this->getColumnDDL($column); } if ($table->hasPrimaryKey()) { $lines[] = $this->getPrimaryKeyDDL($table); } foreach ($table->getUnices() as $unique) { $lines[] = $this->getUniqueDDL($unique); } foreach ($table->getIndices() as $index) { $lines[] = $this->getIndexDDL($index); } if ($this->supportsForeignKeys($table)) { foreach ($table->getForeignKeys() as $foreignKey) { if ($foreignKey->isSkipSql()) { continue; } $lines[] = str_replace("\n ", "\n ", $this->getForeignKeyDDL($foreignKey)); } } $vendorSpecific = $table->getVendorInfoForType('mysql'); if ($vendorSpecific->hasParameter('Type')) { $mysqlTableType = $vendorSpecific->getParameter('Type'); } elseif ($vendorSpecific->hasParameter('Engine')) { $mysqlTableType = $vendorSpecific->getParameter('Engine'); } else { $mysqlTableType = $this->getDefaultTableEngine(); } $tableOptions = $this->getTableOptions($table); if ($table->getDescription()) { $tableOptions[] = 'COMMENT=' . $this->quote($table->getDescription()); } $tableOptions = $tableOptions ? ' ' . implode(' ', $tableOptions) : ''; $sep = ",\n "; $pattern = "\nCREATE TABLE %s\n(\n %s\n) %s=%s%s;\n"; return sprintf($pattern, $this->quoteIdentifier($table->getName()), implode($sep, $lines), $this->getTableEngineKeyword(), $mysqlTableType, $tableOptions); }
public function getAddTableDDL(Table $table) { $tableDescription = $table->hasDescription() ? $this->getCommentLineDDL($table->getDescription()) : ''; $lines = []; foreach ($table->getColumns() as $column) { $lines[] = $this->getColumnDDL($column); } foreach ($table->getUnices() as $unique) { $lines[] = $this->getUniqueDDL($unique); } $sep = ",\n "; $pattern = "\n%sCREATE TABLE %s\n(\n %s\n)%s;\n"; $ret = sprintf($pattern, $tableDescription, $this->quoteIdentifier($table->getName()), implode($sep, $lines), $this->generateBlockStorage($table)); $ret .= $this->getAddPrimaryKeyDDL($table); $ret .= $this->getAddSequencesDDL($table); return $ret; }
public function testRemoveColumn() { $column1 = $this->getColumnMock('id'); $column2 = $this->getColumnMock('title'); $column3 = $this->getColumnMock('isbn'); $table = new Table('books'); $table->addColumn($column1); $table->addColumn($column2); $table->addColumn($column3); $table->removeColumn($column2); $this->assertCount(2, $table->getColumns()); $this->assertTrue($table->hasColumn('id')); $this->assertTrue($table->hasColumn('isbn')); $this->assertFalse($table->hasColumn('title')); }
protected function getAddColumnsComments(Table $table) { $ret = ''; foreach ($table->getColumns() as $column) { $ret .= $this->getAddColumnComment($column); } return $ret; }
/** * Normalizes a table for the current platform. Very important for the TableComparator to not * generate useless diffs. * Useful for checking needed definitions/structures. E.g. Unique Indexes for ForeignKey columns, * which the most Platforms requires but which is not always explicitly defined in the table model. * * @param Table $table The table object which gets modified. */ public function normalizeTable(Table $table) { if ($table->hasForeignKeys()) { foreach ($table->getForeignKeys() as $fk) { if ($fk->getForeignTable() && !$fk->getForeignTable()->isUnique($fk->getForeignColumnObjects())) { $unique = new Unique(); $unique->setColumns($fk->getForeignColumnObjects()); $fk->getForeignTable()->addUnique($unique); } } } if (!$this->supportsIndexSize() && $table->getIndices()) { // when the plafform does not support index sizes we reset it foreach ($table->getIndices() as $index) { $index->resetColumnsSize(); } } foreach ($table->getColumns() as $column) { if ($column->getSize() && ($defaultSize = $this->getDefaultTypeSize($column->getType()))) { if (null === $column->getScale() && intval($column->getSize()) === $defaultSize) { $column->setSize(null); } } } }
public function testRemoveColumnFixesPositions() { $table = new Table(); $col1 = new Column('Foo1'); $table->addColumn($col1); $col2 = new Column('Foo2'); $table->addColumn($col2); $col3 = new Column('Foo3'); $table->addColumn($col3); $this->assertEquals(1, $col1->getPosition()); $this->assertEquals(2, $col2->getPosition()); $this->assertEquals(3, $col3->getPosition()); $this->assertEquals(array(0, 1, 2), array_keys($table->getColumns())); $table->removeColumn($col2); $this->assertEquals(1, $col1->getPosition()); $this->assertEquals(2, $col3->getPosition()); $this->assertEquals(array(0, 1), array_keys($table->getColumns())); }
/** * Appends the generated <table> XML node to its parent node. * * @param Table $table The Table model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendTableNode(Table $table, \DOMNode $parentNode) { $tableNode = $parentNode->appendChild($this->document->createElement('table')); $tableNode->setAttribute('name', $table->getCommonName()); $database = $table->getDatabase(); $schema = $table->getSchema(); if ($schema && $schema !== $database->getSchema()) { $tableNode->setAttribute('schema', $schema); } if (IdMethod::NO_ID_METHOD !== ($idMethod = $table->getIdMethod())) { $tableNode->setAttribute('idMethod', $idMethod); } if ($phpName = $table->getPhpName()) { $tableNode->setAttribute('phpName', $phpName); } $package = $table->getPackage(); if ($package && !$table->isPackageOverriden()) { $tableNode->setAttribute('package', $package); } if ($namespace = $table->getNamespace()) { $tableNode->setAttribute('namespace', $namespace); } if ($table->isSkipSql()) { $tableNode->setAttribute('skipSql', 'true'); } if ($table->isAbstract()) { $tableNode->setAttribute('abstract', 'true'); } if ($interface = $table->getInterface()) { $tableNode->setAttribute('interface', $interface); } if ($table->isCrossRef()) { $tableNode->setAttribute('isCrossRef', 'true'); } $phpNamingMethod = $table->getPhpNamingMethod(); if ($phpNamingMethod && $phpNamingMethod !== $database->getDefaultPhpNamingMethod()) { $tableNode->setAttribute('phpNamingMethod', $phpNamingMethod); } if ($baseClass = $table->getBaseClass()) { $tableNode->setAttribute('baseClass', $baseClass); } if ($baseQueryClass = $table->getBaseQueryClass()) { $tableNode->setAttribute('baseQueryClass', $baseQueryClass); } if ($table->isReadOnly()) { $tableNode->setAttribute('readOnly', 'true'); } if ($table->isReloadOnInsert()) { $tableNode->setAttribute('reloadOnInsert', 'true'); } if ($table->isReloadOnUpdate()) { $tableNode->setAttribute('reloadOnUpdate', 'true'); } if (null !== ($referenceOnly = $table->isForReferenceOnly())) { $tableNode->setAttribute('forReferenceOnly', $referenceOnly ? 'true' : 'false'); } if ($alias = $table->getAlias()) { $tableNode->setAttribute('alias', $alias); } if ($description = $table->getDescription()) { $tableNode->setAttribute('description', $description); } $defaultStringFormat = $table->getDefaultStringFormat(); if (Table::DEFAULT_STRING_FORMAT !== $defaultStringFormat) { $tableNode->setAttribute('defaultStringFormat', $defaultStringFormat); } $defaultAccessorVisibility = $table->getDefaultAccessorVisibility(); if ($defaultAccessorVisibility !== Table::VISIBILITY_PUBLIC) { $tableNode->setAttribute('defaultAccessorVisibility', $defaultAccessorVisibility); } $defaultMutatorVisibility = $table->getDefaultMutatorVisibility(); if ($defaultMutatorVisibility !== Table::VISIBILITY_PUBLIC) { $tableNode->setAttribute('defaultMutatorVisibility', $defaultMutatorVisibility); } foreach ($table->getColumns() as $column) { $this->appendColumnNode($column, $tableNode); } foreach ($table->getForeignKeys() as $foreignKey) { $this->appendForeignKeyNode($foreignKey, $tableNode); } foreach ($table->getIdMethodParameters() as $parameter) { $this->appendIdMethodParameterNode($parameter, $tableNode); } foreach ($table->getIndices() as $index) { $this->appendIndexNode($index, $tableNode); } foreach ($table->getUnices() as $index) { $this->appendUniqueIndexNode($index, $tableNode); } foreach ($table->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $tableNode); } foreach ($table->getBehaviors() as $behavior) { $this->appendBehaviorNode($behavior, $tableNode); } }