Skip to content

Commit febfc7c

Browse files
committed
Use SQL typecasting for Array persistence (#804)
1 parent f501663 commit febfc7c

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"homepage": "https://mvorisek.cz/"
3333
}
3434
],
35-
"version": "2.3.5",
35+
"version": "2.3.6",
3636
"minimum-stability": "dev",
3737
"prefer-stable": true,
3838
"config": {

src/Persistence/Array_.php

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ private function addIdToLoadRow(Model $model, array &$row, $id): void
8484
}
8585
}
8686

87+
public function typecastSaveRow(Model $model, array $row): array
88+
{
89+
$sqlPersistence = (new \ReflectionClass(Sql::class))->newInstanceWithoutConstructor();
90+
91+
return $sqlPersistence->typecastSaveRow($model, $row);
92+
}
93+
94+
public function typecastLoadRow(Model $model, array $row): array
95+
{
96+
$sqlPersistence = (new \ReflectionClass(Sql::class))->newInstanceWithoutConstructor();
97+
98+
return $sqlPersistence->typecastLoadRow($model, $row);
99+
}
100+
87101
/**
88102
* {@inheritdoc}
89103
*/

tests/ContainsManyTest.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,32 @@ public function testNestedContainsMany()
268268

269269
// let's test how it all looks in persistence without typecasting
270270
$exp_lines = $i->setOrder('id')->export(null, null, false)[0]['lines'];
271+
$formatDtForCompareFunc = function (\DateTimeInterface $dt): string {
272+
$dt = (clone $dt)->setTimeZone(new \DateTimeZone('UTC')); // @phpstan-ignore-line
273+
274+
return $dt->format('Y-m-d H:i:s.u');
275+
};
271276
$this->assertSame(
272277
json_encode([
273278
'1' => [
274-
'id' => 1, 'vat_rate_id' => '1', 'price' => '10', 'qty' => '2', 'add_date' => (new \DateTime('2019-06-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
275-
'1' => ['id' => 1, 'percent' => '5', 'valid_till' => (new \DateTime('2019-07-15'))->format('Y-m-d\TH:i:sP')],
276-
'2' => ['id' => 2, 'percent' => '10', 'valid_till' => (new \DateTime('2019-07-30'))->format('Y-m-d\TH:i:sP')],
279+
'id' => 1,
280+
'vat_rate_id' => 1,
281+
'price' => 10,
282+
'qty' => 2,
283+
'add_date' => $formatDtForCompareFunc(new \DateTime('2019-06-01')),
284+
'discounts' => json_encode([
285+
'1' => ['id' => 1, 'percent' => 5, 'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-07-15'))],
286+
'2' => ['id' => 2, 'percent' => 10, 'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-07-30'))],
277287
]),
278288
],
279289
'2' => [
280-
'id' => 2, 'vat_rate_id' => '2', 'price' => '15', 'qty' => '5', 'add_date' => (new \DateTime('2019-07-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
281-
'1' => ['id' => 1, 'percent' => '20', 'valid_till' => (new \DateTime('2019-12-31'))->format('Y-m-d\TH:i:sP')],
290+
'id' => 2,
291+
'vat_rate_id' => 2,
292+
'price' => 15,
293+
'qty' => 5,
294+
'add_date' => $formatDtForCompareFunc(new \DateTime('2019-07-01')),
295+
'discounts' => json_encode([
296+
'1' => ['id' => 1, 'percent' => 20, 'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-12-31'))],
282297
]),
283298
],
284299
]),

tests/ContainsOneTest.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function testContainsOne()
142142
$this->assertFalse($a->loaded());
143143

144144
// now store some address
145-
$a->setMulti($row = ['id' => 1, 'country_id' => 1, 'address' => 'foo', 'built_date' => new \DateTime('2019-01-01 UTC'), 'tags' => ['foo', 'bar'], 'door_code' => null]);
145+
$a->setMulti($row = ['id' => 1, 'country_id' => 1, 'address' => 'foo', 'built_date' => new \DateTime('2019-01-01'), 'tags' => ['foo', 'bar'], 'door_code' => null]);
146146
$a->save();
147147

148148
// now reload invoice and see if it is saved
@@ -156,7 +156,7 @@ public function testContainsOne()
156156

157157
// now add nested containsOne - DoorCode
158158
$c = $i->ref('addr')->ref('door_code');
159-
$c->setMulti($row = ['id' => 1, 'code' => 'ABC', 'valid_till' => new \DateTime('2019-07-01 UTC')]);
159+
$c->setMulti($row = ['id' => 1, 'code' => 'ABC', 'valid_till' => new \DateTime('2019-07-01')]);
160160
$c->save();
161161
$this->assertEquals($row, $i->ref('addr')->ref('door_code')->get());
162162

@@ -174,8 +174,24 @@ public function testContainsOne()
174174

175175
// let's test how it all looks in persistence without typecasting
176176
$exp_addr = $i->setOrder('id')->export(null, null, false)[0]['addr'];
177+
$formatDtForCompareFunc = function (\DateTimeInterface $dt): string {
178+
$dt = (clone $dt)->setTimeZone(new \DateTimeZone('UTC')); // @phpstan-ignore-line
179+
180+
return $dt->format('Y-m-d H:i:s.u');
181+
};
177182
$this->assertSame(
178-
'{"id":1,"country_id":"2","address":"bar","built_date":"2019-01-01T00:00:00+00:00","tags":"[\"foo\",\"bar\"]","door_code":"{\"id\":1,\"code\":\"DEF\",\"valid_till\":\"2019-07-01T00:00:00+00:00\"}"}',
183+
json_encode([
184+
'id' => 1,
185+
'country_id' => 2,
186+
'address' => 'bar',
187+
'built_date' => $formatDtForCompareFunc(new \DateTime('2019-01-01')),
188+
'tags' => json_encode(['foo', 'bar']),
189+
'door_code' => json_encode([
190+
'id' => 1,
191+
'code' => 'DEF',
192+
'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-07-01')),
193+
]),
194+
]),
179195
$exp_addr
180196
);
181197

tests/PersistentArrayOfStringsTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ public function testTypecasting()
5757
'id' => 1,
5858
'string' => 'TwoLines',
5959
'text' => "Two\nLines",
60-
'integer' => '123',
61-
'money' => '123.45',
62-
'float' => '123.456789',
63-
'boolean' => '1',
64-
'boolean_enum' => '0',
60+
'integer' => 123,
61+
'money' => 123.45,
62+
'float' => 123.456789,
63+
'boolean' => 1,
64+
'boolean_enum' => 'N',
6565
'date' => '2019-01-20',
66-
'datetime' => '2019-01-20T12:23:34+00:00',
67-
'time' => '12:23:34',
66+
'datetime' => '2019-01-20 12:23:34.000000',
67+
'time' => '12:23:34.000000',
6868
'array' => '{"foo":"bar","int":123,"rows":["a","b"]}',
6969
'object' => '{"foo":"bar","int":123,"rows":["a","b"]}',
7070
]], $data);

0 commit comments

Comments
 (0)