Skip to content

Commit

Permalink
Add Table::defaultValues()
Browse files Browse the repository at this point in the history
This makes it easy to get the default values from a schema table. Having
this is really useful when you want to make an entity with the default
values from the database.

Refs #4398
  • Loading branch information
markstory committed Aug 31, 2014
1 parent 86b89e6 commit 92627d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Database/Schema/Table.php
Expand Up @@ -334,6 +334,21 @@ public function columnType($name, $type = null) {
return $this->_columns[$name]['type'];
}

/**
* Get a hash of columns and their default values.
*
* @return array
*/
public function defaultValues() {
$defaults = [];
foreach ($this->_columns as $name => $data) {
if (isset($data['default'])) {
$defaults[$name] = $data['default'];
}
}
return $defaults;
}

/**
* Add an index.
*
Expand Down
26 changes: 25 additions & 1 deletion tests/TestCase/Database/Schema/TableTest.php
Expand Up @@ -150,9 +150,33 @@ public function testAddColumnFiltersAttributes() {
}

/**
* Test adding an constraint.
* Test reading default values.
*
* @return void
*/
public function testDefaultValues() {
$table = new Table('articles');
$table->addColumn('id', [
'type' => 'integer',
'default' => 0
])->addColumn('title', [
'type' => 'string',
'default' => 'A title'
])->addColumn('body', [
'type' => 'text',
]);
$result = $table->defaultValues();
$expected = [
'id' => 0,
'title' => 'A title'
];
$this->assertEquals($expected, $result);
}

/**
* Test adding an constraint.
*>
* @return void
*/
public function testAddConstraint() {
$table = new Table('articles');
Expand Down

0 comments on commit 92627d1

Please sign in to comment.