Skip to content

Commit 3ff2595

Browse files
authored
[HOTFIX] - Fix one-to-one relationship bug (#8)
1 parent a33de09 commit 3ff2595

4 files changed

Lines changed: 12 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ $post = [
9090
'author' => [
9191
'id' => 'zxy321',
9292
'name' => 'John Doe'
93-
]
93+
],
9494
'title' => 'Data Mappers Rock',
9595
'content' => 'They save you time',
9696
'comments' => [
@@ -112,6 +112,7 @@ $post = [
112112
],
113113
'content' => 'Nice article!'
114114
],
115+
]
115116
];
116117

117118
$mapper->save($post);
@@ -130,6 +131,3 @@ $mapper->registerHook('updated', function ($table, $key, $updated, $original) {
130131
printf('Table %s (ID: %s) was updated...', $table, implode(':', $key));
131132
});
132133
```
133-
134-
#### Known Issues
135-
* One-to-one mappings where the relationship is associated via primary key do not work as intended

src/Mapping.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ private function replace(array $data, array $original, array $deleteIds = [])
328328
$propertyOriginal = $original[$property->getName()] ?? [];
329329

330330
if (!$property->isCollection()) {
331-
$propertyData = [$propertyData];
332-
$propertyOriginal = [$propertyOriginal];
331+
$propertyData = $propertyData ? [$propertyData] : [];
332+
$propertyOriginal = $propertyOriginal ? [$propertyOriginal] : [];
333333
}
334334

335335
$mapping = new static($this->db, $property->getDefinition(), [$property->getForeignColumn()]);

tests/Fixtures.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ CREATE TABLE items (id INTEGER PRIMARY KEY, order_id INTEGER, description TEXT,
1111
DROP TABLE IF EXISTS discounts;
1212
CREATE TABLE discounts (id INTEGER PRIMARY KEY, order_id INTEGER, description TEXT, amount INTEGER);
1313

14+
DROP TABLE IF EXISTS orders_fulfillments;
15+
CREATE TABLE orders_fulfillments (order_id INTEGER, employee_id INTEGER);
16+
1417
-- Seed database
1518
INSERT INTO customers (id, name) VALUES
1619
(1, 'John Doe'),

tests/MappingTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public function testUpdate()
131131
$customer = $this->getMapping()->eq('id', 1)->findOne();
132132
$original = $customer;
133133

134+
$customer['orders'][0]['fulfillment'] = ['employee_id' => 2];
134135
$customer['orders'][0]['items'][1]['description'] = 'Jumbo Eggs';
135136
$customer['orders'][0]['items'][] = ['id' => 7, 'description' => 'Cheese', 'amount' => 300];
136137

@@ -296,13 +297,17 @@ public function getMapping()
296297
->withColumns('description', 'amount')
297298
->useAutoIncrement();
298299

300+
$fulfillment = (new Definition('orders_fulfillments', ['order_id', 'employee_id']));
301+
299302
$item = (new Definition('items'))
300303
->withColumns('id', 'description', 'amount', 'modified')
301304
->withModificationData(['modified' => '2019-01-02 03:04:05']);
302305

306+
303307
$order = (new Definition('orders'))
304308
->withColumns('id', 'date_created')
305309
->withOne($discount, 'discount', 'order_id')
310+
->withOne($fulfillment, 'fulfillment', 'order_id')
306311
->withMany($item, 'items', 'order_id')
307312
->withDeletionTimestamp('date_deleted');
308313

@@ -343,4 +348,3 @@ public function getReadOnlyMapping()
343348
return new Mapping($this->db, $customer);
344349
}
345350
}
346-

0 commit comments

Comments
 (0)