Skip to content

Commit b6612ec

Browse files
committed
added row mapping support via setRowMapping() callback and mapping config option
1 parent 02a2689 commit b6612ec

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/Bridges/DatabaseDI/DatabaseExtension.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Nette\Schema\Expect;
1313
use Tracy;
1414
use function is_array, is_string;
15+
use Nette\Database\Table\ActiveRow;
1516

1617

1718
/**
@@ -38,6 +39,10 @@ public function getConfigSchema(): Nette\Schema\Schema
3839
'explain' => Expect::bool(true),
3940
'reflection' => Expect::string(), // BC
4041
'conventions' => Expect::string('discovered'), // Nette\Database\Conventions\DiscoveredConventions
42+
'mapping' => Expect::structure([
43+
'convention' => Expect::string(''),
44+
'tables' => Expect::arrayOf('string', 'string'),
45+
])->before(fn($v) => is_string($v) ? ['convention' => $v] : $v),
4146
'autowired' => Expect::bool(),
4247
]),
4348
)->before(fn($val) => is_array(reset($val)) || reset($val) === null
@@ -115,6 +120,15 @@ private function setupDatabase(\stdClass $config, string $name): void
115120
$explorer->addSetup('setConventions', [Nette\DI\Helpers::filterArguments([$config->conventions])[0]]);
116121
}
117122

123+
if ($config->mapping->convention || $config->mapping->tables) {
124+
$explorer->addSetup('setRowMapping', [
125+
new Statement([self::class, 'createRowMapping'], [
126+
$config->mapping->convention,
127+
(array) $config->mapping->tables,
128+
]),
129+
]);
130+
}
131+
118132
$builder->addAlias($this->prefix("$name.connection"), $this->prefix($name));
119133
$builder->addAlias($this->prefix("$name.context"), $this->prefix($name));
120134
$builder->addAlias($this->prefix("$name.explorer"), $this->prefix($name));
@@ -124,4 +138,23 @@ private function setupDatabase(\stdClass $config, string $name): void
124138
$builder->addAlias("nette.database.$name.context", $this->prefix("$name.explorer"));
125139
}
126140
}
141+
142+
143+
public static function createRowMapping(string $convention, array $tables): \Closure
144+
{
145+
return static function (string $table) use ($convention, $tables): string {
146+
if (isset($tables[$table])) {
147+
return $tables[$table];
148+
}
149+
150+
if ($convention !== '') {
151+
$class = str_replace('*', str_replace(' ', '', ucwords(strtr($table, '_', ' '))), $convention);
152+
if (class_exists($class)) {
153+
return $class;
154+
}
155+
}
156+
157+
return ActiveRow::class;
158+
};
159+
}
127160
}

src/Database/Explorer.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,31 @@
77

88
namespace Nette\Database;
99

10-
use JetBrains\PhpStorm\Language;
11-
use Nette;
12-
use Nette\Caching\Cache;
13-
use Nette\Utils\Arrays;
14-
use function func_get_args, str_replace, ucfirst;
10+
use Closure;
1511

1612

1713
/**
1814
* Manages database connection and executes SQL queries.
1915
*/
2016
class Explorer extends Database
2117
{
18+
/** @var ?(Closure(string $table): class-string<Table\ActiveRow>) */
19+
private ?Closure $rowMapping = null;
20+
21+
22+
public function setRowMapping(?Closure $factory): void
23+
{
24+
$this->rowMapping = $factory;
25+
}
26+
27+
2228
/** @internal */
2329
public function createActiveRow(Table\Selection $selection, array $row): Table\ActiveRow
2430
{
25-
return new Table\ActiveRow($row, $selection);
31+
$class = $this->rowMapping
32+
? ($this->rowMapping)($selection->getName())
33+
: Table\ActiveRow::class;
34+
return new $class($row, $selection);
2635
}
2736

2837

0 commit comments

Comments
 (0)