Skip to content

Commit c79a095

Browse files
author
Marco Bunge
committed
Map data to entity by identity if any exists in identity map
1 parent 63a76f2 commit c79a095

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/AbstractMapper.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ abstract class AbstractMapper implements Mapper
2727
*/
2828
protected $tableName;
2929

30+
/**
31+
* @var string
32+
*/
33+
protected $tableNameAlias;
34+
3035
/**
3136
* @var string[]
3237
*/
@@ -279,14 +284,20 @@ public function select(callable $queryCallback, $fields = ['*'], $one = false)
279284
}
280285

281286
$set = [];
287+
$identityMap = $this->getIdentityMap();
288+
282289
foreach ($recordSet as $record) {
283-
// if(isset($record[$this->getLastInsertIdReference()])){
284-
// if($this->getIdentityMap()->hasId($record[$this->getLastInsertIdReference()])){
285-
// $set[] = $this->getIdentityMap()->getObject($record[$this->getLastInsertIdReference()]);
286-
// continue;
287-
// }
288-
// }
289-
$set[] = $this->map($record);
290+
$entity = null;
291+
// fetch entity from identity map
292+
// keeping object id
293+
if(isset($record[$this->getLastInsertIdReference()])){
294+
if($identityMap->hasId($record[$this->getLastInsertIdReference()])){
295+
$entity = $identityMap->getObject($record[$this->getLastInsertIdReference()]);
296+
}
297+
}
298+
299+
// map record to entity
300+
$set[] = $this->map($record, $entity);
290301
}
291302

292303
reset($set);
@@ -528,6 +539,10 @@ private function doDefine()
528539
if (empty($this->tableName)) {
529540
$this->tableName = Inflector::tableize(get_class($this->entityClass));
530541
}
542+
//sanitize and validate table name alias
543+
if (empty($this->tableName)) {
544+
$this->tableNameAlias = substr($this->tableName, 3);
545+
}
531546

532547
}
533548

src/Mapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function getEntityClass();
3636
*/
3737
public function getTableName();
3838

39+
/**
40+
* @return string
41+
*/
42+
public function getTableNameAlias();
43+
3944
/**
4045
* @return string[]
4146
*/

0 commit comments

Comments
 (0)