Skip to content

Commit 3357b99

Browse files
committed
Selection::insert(): refactored record fetch after insert
1 parent ace2256 commit 3357b99

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

src/Database/Table/Selection.php

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ class Selection extends Nette\Object implements \Iterator, IRowContainer, \Array
3636
/** @var string|array|NULL primary key field name */
3737
protected $primary;
3838

39-
/** @var string|bool primary column sequence name, FALSE for autodetection */
39+
/** @var string|bool|NULL primary column sequence name, FALSE for autodetection */
4040
protected $primarySequence = FALSE;
4141

42+
/** @var string|bool|NULL primary key field name with autoincrement */
43+
protected $primarySequenceField = FALSE;
44+
4245
/** @var IRow[] data read from database in [primary key => IRow] format */
4346
protected $rows;
4447

@@ -134,7 +137,7 @@ public function getPrimary($need = TRUE)
134137

135138

136139
/**
137-
* @return string
140+
* @return string|NULL
138141
*/
139142
public function getPrimarySequence()
140143
{
@@ -156,6 +159,23 @@ public function setPrimarySequence($sequence)
156159
return $this;
157160
}
158161

162+
/**
163+
* @return string|NULL
164+
*/
165+
public function getPrimarySequenceField()
166+
{
167+
if ($this->primarySequenceField === FALSE) {
168+
$this->primarySequenceField = NULL;
169+
foreach ($this->context->getStructure()->getColumns($this->name) as $columnMeta) {
170+
if ($columnMeta['autoincrement']) {
171+
$this->primarySequenceField = $columnMeta['name'];
172+
break;
173+
}
174+
}
175+
}
176+
177+
return $this->primarySequenceField;
178+
}
159179

160180
/**
161181
* @return string
@@ -745,32 +765,35 @@ public function insert($data)
745765
return $return->getRowCount();
746766
}
747767

748-
$primarySequenceName = $this->getPrimarySequence();
749-
$primaryKey = $this->context->getInsertId(
750-
!empty($primarySequenceName)
751-
? $this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName)
752-
: $primarySequenceName
753-
);
754-
if ($primaryKey === FALSE) {
755-
unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
756-
return $return->getRowCount();
768+
if (Nette\Utils\Arrays::isList($data)) {
769+
return $data;
757770
}
758771

759-
if (is_array($this->getPrimary())) {
760-
$primaryKey = [];
761-
762-
foreach ((array) $this->getPrimary() as $key) {
763-
if (!isset($data[$key])) {
764-
return $data;
765-
}
766-
772+
$primaryKey = [];
773+
$primarySequenceField = $this->getPrimarySequenceField();
774+
foreach ((array) $this->getPrimary() as $key) {
775+
if (isset($data[$key])) {
767776
$primaryKey[$key] = $data[$key];
768777
}
769-
if (count($primaryKey) === 1) {
770-
$primaryKey = reset($primaryKey);
778+
}
779+
780+
if ($primarySequenceField !== NULL) {
781+
$primarySequenceName = $this->getPrimarySequence();
782+
$primaryKey[$primarySequenceField] = $this->context->getInsertId(
783+
!empty($primarySequenceName)
784+
? $this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName)
785+
: $primarySequenceName
786+
);
787+
if ($primaryKey[$primarySequenceField] === FALSE) {
788+
unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
789+
return $return->getRowCount();
771790
}
772791
}
773792

793+
if (count($primaryKey) === 1) {
794+
$primaryKey = reset($primaryKey);
795+
}
796+
774797
$row = $this->createSelectionInstance()
775798
->select('*')
776799
->wherePrimary($primaryKey)

0 commit comments

Comments
 (0)