diff --git a/src/model/types/DateType.php b/src/model/types/DateType.php new file mode 100644 index 0000000..e2459b2 --- /dev/null +++ b/src/model/types/DateType.php @@ -0,0 +1,8 @@ +requiredColumn(__FUNCTION__, TimestampType::class, $alias); } + public function birthday($alias) + { + return $this->requiredColumn(__FUNCTION__, DateType::class, $alias); + } + public function home_address_id($alias) { return $this->requiredColumn(__FUNCTION__, IntType::class, $alias); diff --git a/test/test-unit.php b/test/test-unit.php index 08d3b4a..50e4ce3 100644 --- a/test/test-unit.php +++ b/test/test-unit.php @@ -15,6 +15,7 @@ use mindplay\sql\model\query\SelectQuery; use mindplay\sql\model\schema\Column; use mindplay\sql\model\types\BoolType; +use mindplay\sql\model\types\DateType; use mindplay\sql\model\types\IntType; use mindplay\sql\model\types\JSONType; use mindplay\sql\model\types\StringType; @@ -754,6 +755,19 @@ function () { } ); +test( + 'can map DATE to Unix timestamps', + function () { + $type = new DateType(); + + $valid_date = '2015-11-04'; + $valid_timestamp = 1446595200; + + eq($type->convertToPHP($valid_date), $valid_timestamp, "can convert to PHP value"); + eq($type->convertToSQL($valid_timestamp), $valid_date, "can convert to SQL DATE value"); + } +); + test( 'can map PHP values to JSON', function () { @@ -840,7 +854,7 @@ function () { $columns = $user->listColumns(); - eq(count($columns), 6); + eq(count($columns), 7); foreach ($columns as $column) { ok($column instanceof Column); @@ -1084,6 +1098,7 @@ function () { 'first_name' => StringType::class, 'last_name' => StringType::class, 'dob' => TimestampType::class, + 'birthday' => DateType::class, 'home_address_id' => IntType::class, 'deleted' => BoolType::class, ];