Skip to content

Commit 56f26a8

Browse files
committed
Set default values for nullable properties (null) across models and handle array-to-object casting in Mapper.
1 parent 5d8e23a commit 56f26a8

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

src/Mapper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ public function getEntity(array $fieldValues = []): object
221221
$instance = new $class();
222222

223223
// The command below is to get all properties of the class.
224-
// This will allow to process all properties, even if they are not in the $fieldValues array.
224+
// This will allow processing all properties, even if they are not in the $fieldValues array.
225225
// Particularly useful for processing the selectFunction.
226226
$fieldValues = array_merge(Serialize::from($instance)->withStopAtFirstLevel()->toArray(), $fieldValues);
227227
ObjectCopy::copy($fieldValues, $instance, new MapFromDbToInstanceHandler($this));
228228

229+
if (is_array($instance)) {
230+
$instance = (object)$instance;
231+
}
232+
229233
return $instance;
230234
}
231235

tests/Model/Class1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
class Class1
66
{
7-
public ?int $id;
7+
public ?int $id = null;
88
}

tests/Model/Class2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Class2
66
{
7-
public ?int $id;
7+
public ?int $id = null;
88

9-
public ?int $idTable1;
9+
public ?int $idTable1 = null;
1010
}

tests/Model/Class3.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class Class3
1010
{
1111
#[FieldAttribute(primaryKey: true)]
12-
public ?int $id;
12+
public ?int $id = null;
1313

1414
#[FieldAttribute(fieldName: "id_table1", parentTable: "table1")]
15-
public ?int $idTable1;
15+
public ?int $idTable1 = null;
1616
}

tests/Model/Class4.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Class4
1212
use DeletedAt;
1313

1414
#[FieldAttribute(primaryKey: true)]
15-
public ?int $id;
15+
public ?int $id = null;
1616

1717
#[FieldAttribute(fieldName: "id_table2", parentTable: "table2")]
18-
public ?int $idTable2;
18+
public ?int $idTable2 = null;
1919
}

0 commit comments

Comments
 (0)