/** * Get Class Properties * @param $item * @return array */ public static function getProperties($item) { // create a reflector $reflector = new \ReflectionClass($item); $columns = array(); // iterate all $property foreach ($reflector->getProperties() as $property) { $document = $property->getDocComment(); $matches = null; // find annotation if (preg_match("/@column\\((?P<type>[^)]+)\\)/", $document, $matches)) { $type = $matches["type"]; $column = new Column(); $column->setType($type); $column->name = $property->name; $columns[] = $column; } } return $columns; }
/** * @param $columnName * @param $type * @param array $options * @return $this */ public function addColumn($columnName, $type = null, $options = []) { if (!$columnName instanceof Column) { $column = new Column(); $column->setName($columnName)->setDataType($type); if (!empty($options)) { $column->setOptions($options); if (isset($options['primaryKey'])) { $this->addPrimaryKey($column); } if (isset($options['foreignKey']) && isset($options['referencedTable']) && isset($options['referencedColumns'])) { $this->addForeignKey($column, $options['referencedTable'], $options['referencedColumns']); } } } else { $column = $columnName; } $this->setColumn($column); return $this; }
/** * {@inheritdoc} */ public function addColumn($tableName, Column $newColumn) { if ($tableName instanceof Table) { $tableName = $tableName->getName(); } $sql = "ALTER TABLE {$this->quote($tableName)} ADD {$this->quote($newColumn->getName())} {$this->getColumnDefinition($newColumn)}"; $after = $newColumn->getAfter(); if (isset($after)) { $sql .= " AFTER {$after}"; } $this->execute($sql); return true; }
/** * @covers \Core\Database\Mapper\MySqlMapper::__construct * @covers \Core\Database\Mapper\MySqlMapper::addColumn * @covers \Core\Database\Column::__construct * @covers \Core\Database\Column::setName * @covers \Core\Database\Column::setDataType * @covers \Core\Database\Column::setDefault * @covers \Core\Database\Column::setAfter */ public function testAddColumn() { $this->mapper = new MySqlMapper($this->getConfig()); $newColumn = new Column(); $newColumn->setName('created_on')->setDataType('timestamp')->setDefault('CURRENT_TIMESTAMP')->setAfter('category'); $result = $this->mapper->addColumn('product', $newColumn); $this->assertTrue($result); }