Skip to content

Commit 669e5c9

Browse files
committed
Making Table::columnType() a setter too
1 parent 804ad32 commit 669e5c9

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Database/Schema/Table.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ class Table {
224224
*/
225225
const ACTION_RESTRICT = 'restrict';
226226

227+
/**
228+
* Foreign key restrict default
229+
*
230+
* @var string
231+
*/
227232
const ACTION_SET_DEFAULT = 'setDefault';
228233

229234
/**
@@ -312,15 +317,20 @@ public function column($name) {
312317
}
313318

314319
/**
315-
* Convenience method for getting the type of a given column.
320+
* Sets the type of a column, or returns its current type
321+
* if none is passed.
316322
*
317323
* @param string $name The column to get the type of.
324+
* @param string $type The type to set the column to.
318325
* @return string|null Either the column type or null.
319326
*/
320-
public function columnType($name) {
327+
public function columnType($name, $type = null) {
321328
if (!isset($this->_columns[$name])) {
322329
return null;
323330
}
331+
if ($type !== null) {
332+
$this->_columns[$name]['type'] = $type;
333+
}
324334
return $this->_columns[$name]['type'];
325335
}
326336

tests/TestCase/Database/Schema/TableTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ public function testColumnType() {
7878
$this->assertNull($table->columnType('not there'));
7979
}
8080

81+
/**
82+
* Test columnType setter method
83+
*
84+
* @return void
85+
*/
86+
public function testColumnTypeSet() {
87+
$table = new Table('articles');
88+
$table->addColumn('title', [
89+
'type' => 'string',
90+
'length' => 25,
91+
'null' => false
92+
]);
93+
$this->assertEquals('string', $table->columnType('title'));
94+
$table->columnType('title', 'json');
95+
$this->assertEquals('json', $table->columnType('title'));
96+
}
97+
8198
/**
8299
* Attribute keys should be filtered and have defaults set.
83100
*

0 commit comments

Comments
 (0)