Skip to content

Commit db4d5f5

Browse files
committed
Set on RelatedRecord
1 parent 43fb4da commit db4d5f5

File tree

5 files changed

+69
-36
lines changed

5 files changed

+69
-36
lines changed

Tests/Unit/InsertTest.php

+31-31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@
44

55
class InsertTest extends \PHPUnit\Framework\TestCase
66
{
7+
public function testDateNullInsert() : void
8+
{
9+
$transaction = new \PHPFUI\ORM\Transaction();
10+
$test = new \Tests\App\Record\DateRecord();
11+
$test->dateRequired = $date = \date('Y-m-d');
12+
$timeStamp = \date('Y-m-d H:i:s');
13+
$id = $test->insert();
14+
$insertedTest = new \Tests\App\Record\DateRecord($id);
15+
$this->assertNull($insertedTest->dateDefaultNull);
16+
$this->assertEquals($date, $insertedTest->dateRequired);
17+
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
18+
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
19+
$this->assertGreaterThanOrEqual($timeStamp, $insertedTest->timestampDefaultCurrentNullable);
20+
$this->assertGreaterThanOrEqual($timeStamp, $insertedTest->timestampDefaultCurrentNotNull);
21+
22+
$this->assertTrue($transaction->rollBack());
23+
}
24+
25+
public function testDateRequiredInsert() : void
26+
{
27+
$this->expectException(\Exception::class);
28+
$transaction = new \PHPFUI\ORM\Transaction();
29+
$test = new \Tests\App\Record\DateRecord();
30+
$id = $test->insert();
31+
$insertedTest = new \Tests\App\Record\DateRecord($id);
32+
$this->assertNull($insertedTest->dateDefaultNull);
33+
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
34+
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
35+
$this->assertTrue($transaction->rollBack());
36+
}
37+
738
public function testMultipleInserts() : void
839
{
940
$transaction = new \PHPFUI\ORM\Transaction();
@@ -73,37 +104,6 @@ public function testMultipleInserts() : void
73104
$this->assertCount(29, $customerTable);
74105
}
75106

76-
public function testDateNullInsert() : void
77-
{
78-
$transaction = new \PHPFUI\ORM\Transaction();
79-
$test = new \Tests\App\Record\DateRecord();
80-
$test->dateRequired = $date = \date('Y-m-d');
81-
$timeStamp = \date('Y-m-d H:i:s');
82-
$id = $test->insert();
83-
$insertedTest = new \Tests\App\Record\DateRecord($id);
84-
$this->assertNull($insertedTest->dateDefaultNull);
85-
$this->assertEquals($date, $insertedTest->dateRequired);
86-
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
87-
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
88-
$this->assertGreaterThanOrEqual($timeStamp, $insertedTest->timestampDefaultCurrentNullable);
89-
$this->assertGreaterThanOrEqual($timeStamp, $insertedTest->timestampDefaultCurrentNotNull);
90-
91-
$this->assertTrue($transaction->rollBack());
92-
}
93-
94-
public function testDateRequiredInsert() : void
95-
{
96-
$this->expectException(\Exception::class);
97-
$transaction = new \PHPFUI\ORM\Transaction();
98-
$test = new \Tests\App\Record\DateRecord();
99-
$id = $test->insert();
100-
$insertedTest = new \Tests\App\Record\DateRecord($id);
101-
$this->assertNull($insertedTest->dateDefaultNull);
102-
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
103-
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
104-
$this->assertTrue($transaction->rollBack());
105-
}
106-
107107
public function testRecordInsert() : void
108108
{
109109
$customer = new \Tests\App\Record\Customer();

docs/5. Virtual Fields.md

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ echo 'Packed By : ' . $order->packedBy->initials() . "\n";
4848
echo 'Inspected By : ' . $order->inspectedBy->first_name . "\n";
4949
```
5050

51+
You can also assign records to the related record if it the right type.
52+
```php
53+
$employee = new \App\Record\Employee(['last_name' => 'Zare']);
54+
$order->salesPerson = $employee;
55+
```
56+
5157
## Custom Virtual Fields
5258
You can write custom classes to create virtual fields on any record. Here we are adding a **gross** virtual to the OrderDetail record.
5359
```php

src/PHPFUI/ORM/RelatedRecord.php

+18
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,22 @@ public function getValue(array $parameters) : mixed
1010

1111
return new $class($this->currentRecord[$this->fieldName . \PHPFUI\ORM::$idSuffix]);
1212
}
13+
14+
public function setValue(mixed $value, array $parameters) : void
15+
{
16+
$class = \array_shift($parameters);
17+
18+
if (! ($value instanceof $class))
19+
{
20+
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' is not an instance of ' . $class);
21+
}
22+
$primaryKevValues = $value->getPrimaryKeyValues();
23+
24+
if (1 != \count($primaryKevValues))
25+
{
26+
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' does not have a single primary key');
27+
}
28+
29+
$this->currentRecord[$this->fieldName . \PHPFUI\ORM::$idSuffix] = \array_shift($primaryKeyValues);
30+
}
1331
}

src/PHPFUI/ORM/Table.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,9 @@ public function insert(array $records) : bool
708708
$tableName = $this->getTableName();
709709
$sql = "insert into `{$tableName}` (";
710710

711-
$fields = array_keys($this->getFields());
711+
$fields = \array_keys($this->getFields());
712712
$comma = '';
713+
713714
foreach ($fields as $fieldName)
714715
{
715716
$sql .= "{$comma}`{$fieldName}`";
@@ -720,14 +721,17 @@ public function insert(array $records) : bool
720721

721722
$input = [];
722723
$comma = '(';
724+
723725
foreach ($records as $record)
724726
{
725727
if ($record->getTableName() != $tableName)
726728
{
727-
$myType = get_debug_type($this->instance);
728-
$haveType = get_debug_type($record);
729+
$myType = \get_debug_type($this->instance);
730+
$haveType = \get_debug_type($record);
731+
729732
throw new \PHPFUI\ORM\Exception(__METHOD__ . ": record should be of type {$myType} but is of type {$haveType}");
730733
}
734+
731735
foreach ($fields as $fieldName)
732736
{
733737
$sql .= $comma . '?';
@@ -742,7 +746,7 @@ public function insert(array $records) : bool
742746
$this->lastInput = $input;
743747
\PHPFUI\ORM::execute($this->lastSql, $this->lastInput);
744748

745-
return \PHPFUI\ORM::getLastErrorCode() == 0;
749+
return 0 == \PHPFUI\ORM::getLastErrorCode();
746750
}
747751

748752
public function setDistinct(string $distinct = 'DISTINCT') : static

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ private function getComment(\PHPFUI\ORM\Schema\Field $field, array &$commentedFi
229229
if (! isset($commentedFields[$var]))
230230
{
231231
$table = \PHPFUI\ORM::getBaseClassName($var);
232-
$block .= "\n * @property \\~~RECORD_NAMESPACE~~\\" . $table . ' $' . $var . ' related record';
232+
$className = '\\' . \PHPFUI\ORM::$recordNamespace . "\\{$table}";
233+
234+
if (\class_exists($className))
235+
{
236+
$block .= "\n * @property \\~~RECORD_NAMESPACE~~\\" . $table . ' $' . $var . ' related record';
237+
}
233238
}
234239
$commentedFields[$var] = true;
235240
}

0 commit comments

Comments
 (0)