Skip to content

Commit 3c866ba

Browse files
committed
Fixed
1 parent 6e40e33 commit 3c866ba

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/EntityManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ protected function markAsSaved(DataMapper $data, $id): bool
302302
$this->dehidrated = true;
303303
$this->isNew = false;
304304
$this->modified = [];
305+
if(!empty($this->pendingLinks)){
306+
$this->executePendingLinkage();
307+
}
305308
return true;
306309
};
307310
}

src/ORM/DataMapper.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class DataMapper
6464
/** @var bool */
6565
protected $deleted = false;
6666

67+
/** @var array */
68+
protected $pendingLinks = [];
69+
6770
/**
6871
* DataMapper constructor.
6972
* @param EntityManager $entityManager
@@ -312,6 +315,15 @@ public function link(string $relation, $items)
312315
throw new RuntimeException("Unsupported relation type");
313316
}
314317

318+
if($this->isNew){
319+
$this->pendingLinks[] = [
320+
'relation' => $rel,
321+
'items' => $items,
322+
'link' => true,
323+
];
324+
return;
325+
}
326+
315327
$rel->link($this, $items);
316328
}
317329

@@ -331,6 +343,15 @@ public function unlink(string $relation, $items)
331343
throw new RuntimeException("Unsupported relation type");
332344
}
333345

346+
if($this->isNew){
347+
$this->pendingLinks[] = [
348+
'relation' => $rel,
349+
'items' => $items,
350+
'link' => false,
351+
];
352+
return;
353+
}
354+
334355
$rel->unlink($this, $items);
335356
}
336357

@@ -464,4 +485,19 @@ protected function getRelationResult(Relation $relation, callable $callback = nu
464485

465486
return $closure->call($relation, $this, $callback);
466487
}
488+
489+
protected function executePendingLinkage()
490+
{
491+
foreach ($this->pendingLinks as $item){
492+
/** @var HasOneOrManyThrough $rel */
493+
$rel = $item['relation'];
494+
if($item['link']){
495+
$rel->link($this, $item['items']);
496+
} else {
497+
$rel->unlink($this, $item['items']);
498+
}
499+
}
500+
501+
$this->pendingLinks = [];
502+
}
467503
}

src/ORM/Relation/HasOneOrManyThrough.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function link(DataMapper $data, $items)
9999
};
100100

101101
foreach ($items as $item){
102-
$val2 = is_subclass_of($item, $this->entityClass, false) ? $extractor->call($item) : $item;
102+
$val2 = is_a($item, $this->entityClass, false) ? $extractor->call($item) : $item;
103103
try{
104104

105105
(new Insert($connection))->insert([
@@ -109,6 +109,7 @@ public function link(DataMapper $data, $items)
109109

110110
}catch (\Exception $e){
111111
// Ignore
112+
die('uuu');
112113
}
113114
}
114115
}
@@ -154,7 +155,7 @@ public function unlink(DataMapper $data, $items)
154155
};
155156

156157
foreach ($items as $item){
157-
$val2[] = is_subclass_of($item, $this->entityClass, false) ? $extractor->call($item) : $item;
158+
$val2[] = is_a($item, $this->entityClass, false) ? $extractor->call($item) : $item;
158159
}
159160

160161
try{

0 commit comments

Comments
 (0)