Skip to content

Commit 0664e51

Browse files
committed
Better default handling
1 parent db4d5f5 commit 0664e51

File tree

4 files changed

+10
-19
lines changed

4 files changed

+10
-19
lines changed

Tests/Unit/InsertTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ public function testDateNullInsert() : void
2424

2525
public function testDateRequiredInsert() : void
2626
{
27-
$this->expectException(\Exception::class);
28-
$transaction = new \PHPFUI\ORM\Transaction();
2927
$test = new \Tests\App\Record\DateRecord();
3028
$id = $test->insert();
29+
$this->assertNotEmpty(\PHPFUI\ORM::getLastError());
30+
$this->assertEquals(0, $id);
3131
$insertedTest = new \Tests\App\Record\DateRecord($id);
3232
$this->assertNull($insertedTest->dateDefaultNull);
3333
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
3434
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
35-
$this->assertTrue($transaction->rollBack());
3635
}
3736

3837
public function testMultipleInserts() : void
@@ -197,11 +196,10 @@ public function testRelatedInsert() : void
197196

198197
public function testRequiredStringNotSetInsert() : void
199198
{
200-
$this->expectException(\Exception::class);
201-
$transaction = new \PHPFUI\ORM\Transaction();
202199
$test = new \Tests\App\Record\StringRecord();
203200
$id = $test->insert();
204-
$this->assertTrue($transaction->rollBack());
201+
$this->assertNotEmpty(\PHPFUI\ORM::getLastError());
202+
$this->assertEquals(0, $id);
205203
}
206204

207205
public function testStringNullInsert() : void

src/PHPFUI/ORM/Record.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,7 @@ public function setEmpty() : static
558558

559559
foreach (static::$fields as $field => $description)
560560
{
561-
if (\array_key_exists(self::DEFAULT_INDEX, $description))
562-
{
563-
$this->current[$field] = $description[self::DEFAULT_INDEX];
564-
}
561+
$this->current[$field] = $description[self::DEFAULT_INDEX] ?? null;
565562
}
566563

567564
return $this;

src/PHPFUI/ORM/RelatedRecord.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public function setValue(mixed $value, array $parameters) : void
1919
{
2020
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' is not an instance of ' . $class);
2121
}
22-
$primaryKevValues = $value->getPrimaryKeyValues();
22+
$primaryKeyValues = $value->getPrimaryKeyValues();
2323

24-
if (1 != \count($primaryKevValues))
24+
if (1 != \count($primaryKeyValues))
2525
{
2626
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' does not have a single primary key');
2727
}

src/PHPFUI/ORM/Tool/Generate/CRUD.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,13 @@ protected function getLine(\PHPFUI\ORM\Schema\Field $field) : string
161161
case 'bool':
162162
case 'datetime':
163163
case 'string':
164-
if ('NULL' === $field->defaultValue)
164+
if ('NULL' === $field->defaultValue || 'CURRENT_TIMESTAMP' == $field->defaultValue || 'CURRENT_DATE' == $field->defaultValue)
165165
{
166166
$defaultValue = 'NULL';
167167
}
168-
elseif ('CURRENT_TIMESTAMP' == $field->defaultValue || 'CURRENT_DATE' == $field->defaultValue)
168+
elseif (\str_contains($field->defaultValue ?? '', "'") || \str_contains($field->defaultValue ?? '', '"'))
169169
{
170-
$defaultValue = null;
171-
}
172-
elseif (\str_contains($field->defaultValue ?? '', "'"))
173-
{
174-
$defaultValue = "\"{$field->defaultValue}\"";
170+
$defaultValue = $field->defaultValue;
175171
}
176172
elseif (null !== $field->defaultValue)
177173
{

0 commit comments

Comments
 (0)