/**
  * Adds Columns to the specified table.
  *
  * @param Table $table The Table model class to add columns to.
  */
 protected function addColumns(Table $table)
 {
     $stmt = $this->dbh->query("SELECT COLUMN_NAME, DATA_TYPE, NULLABLE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, DATA_DEFAULT FROM USER_TAB_COLS WHERE TABLE_NAME = '" . $table->getName() . "'");
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         if (false !== strpos($row['COLUMN_NAME'], '$')) {
             // this is an Oracle internal column - prune
             continue;
         }
         $size = $row['DATA_PRECISION'] ? $row['DATA_PRECISION'] : $row['DATA_LENGTH'];
         $scale = $row['DATA_SCALE'];
         $default = $row['DATA_DEFAULT'];
         $type = $row['DATA_TYPE'];
         $isNullable = 'Y' === $row['NULLABLE'];
         if ($type === 'NUMBER' && $row['DATA_SCALE'] > 0) {
             $type = 'DECIMAL';
         }
         if ($type === 'NUMBER' && $size > 9) {
             $type = 'BIGINT';
         }
         if ($type === 'FLOAT' && $row['DATA_PRECISION'] == 126) {
             $type = 'DOUBLE';
         }
         if (false !== strpos($type, 'TIMESTAMP(')) {
             $type = substr($type, 0, strpos($type, '('));
             $default = '0000-00-00 00:00:00';
             $size = null;
             $scale = null;
         }
         if ('DATE' === $type) {
             $default = '0000-00-00';
             $size = null;
             $scale = null;
         }
         $propelType = $this->getMappedPropelType($type);
         if (!$propelType) {
             $propelType = Column::DEFAULT_TYPE;
             $this->warn('Column [' . $table->getName() . '.' . $row['COLUMN_NAME'] . '] has a column type (' . $row['DATA_TYPE'] . ') that Propel does not support.');
         }
         $column = new Column($row['COLUMN_NAME']);
         $column->setPhpName();
         // Prevent problems with strange col names
         $column->setTable($table);
         $column->setDomainForType($propelType);
         $column->getDomain()->setOriginSqlType($type);
         $column->getDomain()->replaceSize($size);
         $column->getDomain()->replaceScale($scale);
         if ($default !== null) {
             $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE));
         }
         $column->setAutoIncrement(false);
         // This flag sets in self::parse()
         $column->setNotNull(!$isNullable);
         $table->addColumn($column);
     }
 }
示例#2
0
 public function testSetCustomPhpName()
 {
     $column = new Column('created_at');
     $column->setPhpName('CreatedAt');
     $this->assertSame('CreatedAt', $column->getPhpName());
     $this->assertSame('createdAt', $column->getStudlyPhpName());
 }
 public function testPhpSingularName()
 {
     $column = new Column();
     $column->setPhpName('Aliases');
     $this->assertEquals($column->getPhpName(), 'Aliases');
     $this->assertEquals($column->getPhpSingularName(), 'Aliase');
     $column = new Column();
     $column->setPhpName('Aliases');
     $column->setPhpSingularName('Alias');
     $this->assertEquals($column->getPhpName(), 'Aliases');
     $this->assertEquals($column->getPhpSingularName(), 'Alias');
 }
示例#4
0
 public function testValidateReturnsFalseWhenTwoColumnssHaveSamePhpName()
 {
     $column1 = new Column('foo');
     $column2 = new Column('bar');
     $column2->setPhpName('Foo');
     $table = new Table('foo_table');
     $table->addColumn($column1);
     $table->addColumn($column2);
     $appData = $this->getAppDataForTable($table);
     $validator = new SchemaValidator($appData);
     $this->assertFalse($validator->validate());
     $this->assertContains('Column "bar" declares a phpName already used in table "foo_table"', $validator->getErrors());
 }