Skip to content

Commit 5c90472

Browse files
committed
Construct Record from DataObject
1 parent 9c1f64f commit 5c90472

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Tests/Unit/SelectTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ public function testSelectCount() : void
1111
$recordCursor = $table->getRecordCursor();
1212
$this->assertEquals(29, $recordCursor->count());
1313
$this->assertEquals(29, \count($recordCursor));
14-
$this->assertEquals('Company A', $recordCursor->current()->company);
14+
$customer = $recordCursor->current();
15+
$this->assertEquals('Company A', $customer->company);
1516

1617
$arrayCursor = $table->getArrayCursor();
1718
$this->assertEquals(29, $arrayCursor->count());
1819
$this->assertEquals(29, \count($arrayCursor));
1920
$this->assertEquals('Company A', $arrayCursor->current()['company']);
21+
$arrayRecord = new \Tests\App\Record\Customer();
22+
$arrayRecord->setFrom($arrayCursor->current());
23+
$this->assertEquals($customer->toArray(), $arrayRecord->toArray());
2024

2125
$dataObjectCursor = $table->getDataObjectCursor();
2226
$this->assertEquals(29, $dataObjectCursor->count());
2327
$this->assertEquals(29, \count($dataObjectCursor));
24-
$this->assertEquals('Company A', $dataObjectCursor->current()->company);
28+
$dataObject = $dataObjectCursor->current();
29+
$this->assertEquals('Company A', $dataObject->company);
30+
$dataRecord = new \Tests\App\Record\Customer($dataObject);
31+
$this->assertEquals($customer->toArray(), $dataRecord->toArray());
2532
}
2633

2734
public function testSelectGroupBy() : void

docs/2. Active Record.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ A **Record** constructor attempts to read the specified row from the table. It c
2929
- **int** primary key value, will load object values if the primary key value exists.
3030
- **string** primary key value, will load object values if the primary key value exists.
3131
- **array** record is attempted to be read from database using the values of the fields provided.
32+
- **\PHPFUI\ORM\DataObject** record is constructed from an existing DataObject
3233
- **null** (default) constructs an empty object.
3334

3435
Both int and string parameters to the constructor are type checked. Calling the constructor with a parameter can be see as the same as the following, but with type checking:

src/PHPFUI/ORM/Record.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ abstract class Record extends DataObject
5959
* - **int** primary key value, will load object values if the primary key value exists
6060
* - **string** primary key value, will load object values if the primary key value exists
6161
* - **array** record is attempted to be read from database using the values of the fields provided.
62+
* - **\PHPFUI\ORM\DataObject** record is constructed from an existing DataObject
6263
* - **null** (default) constructs an empty object
6364
*
6465
* @param int|array<string,mixed>|null|string $parameter
6566
*/
66-
public function __construct(int|array|null|string $parameter = null)
67+
public function __construct(int|array|null|string|\PHPFUI\ORM\DataObject $parameter = null)
6768
{
6869
$this->setEmpty();
69-
$type = \gettype($parameter);
70+
$type = \get_debug_type($parameter);
7071

7172
switch ($type)
7273
{
@@ -83,7 +84,7 @@ public function __construct(int|array|null|string $parameter = null)
8384

8485
break;
8586

86-
case 'integer':
87+
case 'int':
8788

8889
if (1 == \count(static::$primaryKeys) && 'int' == static::$fields[static::$primaryKeys[0]][self::PHP_TYPE_INDEX])
8990
{
@@ -101,6 +102,12 @@ public function __construct(int|array|null|string $parameter = null)
101102
$this->read($parameter);
102103

103104
break;
105+
106+
case \PHPFUI\ORM\DataObject::class:
107+
$this->current = array_intersect_key($parameter->current, static::$fields);
108+
109+
break;
110+
104111
}
105112
}
106113

0 commit comments

Comments
 (0)