1212use Nette \Schema \Expect ;
1313use Tracy ;
1414use 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}
0 commit comments