*/ class FormBuilder { /** * Build a form based on the given table. * * @param BundleInterface $bundle * @param Table $table * @param string $formTypeNamespace * * @return string */ public function buildFormType(BundleInterface $bundle, Table $table, $formTypeNamespace) { $modelName = $table->getPhpName(); $formTypeContent = file_get_contents(__DIR__ . '/../Resources/skeleton/FormType.php'); $formTypeContent = str_replace('##NAMESPACE##', $bundle->getNamespace() . str_replace('/', '\\', $formTypeNamespace), $formTypeContent); $formTypeContent = str_replace('##CLASS##', $modelName . 'Type', $formTypeContent); $formTypeContent = str_replace('##FQCN##', sprintf('%s\%s', $table->getNamespace(), $modelName), $formTypeContent); $formTypeContent = str_replace('##TYPE_NAME##', strtolower($modelName), $formTypeContent); $formTypeContent = str_replace('##BUILD_CODE##', $this->buildFormFields($table), $formTypeContent); return $formTypeContent; } /** * 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; } }