88namespace Nette \Database \Table ;
99
1010use Nette ;
11- use function array_intersect_key , array_key_exists , array_keys , implode , is_array , iterator_to_array ;
11+ use function array_intersect_key , array_key_exists , array_keys , array_map , implode , is_array , iterator_to_array ;
1212
1313
1414/**
1919class ActiveRow implements \IteratorAggregate, IRow
2020{
2121 private bool $ dataRefreshed = false ;
22+ private readonly ?Nette \Database \EntityMapping $ entityMapping ;
2223
2324
2425 public function __construct (
@@ -27,6 +28,7 @@ public function __construct(
2728 /** @var Selection<ActiveRow> */
2829 private Selection $ table ,
2930 ) {
31+ $ this ->entityMapping = $ table ->getExplorer ()->getEntityMapping ();
3032 }
3133
3234
@@ -66,12 +68,23 @@ public function __toString(): string
6668 public function toArray (): array
6769 {
6870 $ this ->accessColumn (null );
71+ $ entityMapping = $ this ->entityMapping ;
72+ if ($ entityMapping ) {
73+ $ translated = [];
74+ foreach ($ this ->data as $ key => $ value ) {
75+ $ translated [$ entityMapping ->getPropertyName ($ key )] = $ value ;
76+ }
77+ return $ translated ;
78+ }
6979 return $ this ->data ;
7080 }
7181
7282
7383 /**
7484 * Returns primary key value, or an array of values for composite primary keys.
85+ * Composite key arrays are keyed by database column names (unlike toArray(),
86+ * which uses property names) so the result can be passed directly to
87+ * Selection::wherePrimary().
7588 */
7689 public function getPrimary (bool $ throw = true ): mixed
7790 {
@@ -163,7 +176,10 @@ public function update(iterable $data): bool
163176 ->wherePrimary ($ primary );
164177
165178 if ($ selection ->update ($ data )) {
166- if ($ tmp = array_intersect_key ($ data , $ primary )) {
179+ $ columnData = $ this ->entityMapping
180+ ? Nette \Database \Helpers::translateColumns ($ data , $ this ->entityMapping )
181+ : $ data ;
182+ if ($ tmp = array_intersect_key ($ columnData , $ primary )) {
167183 $ selection = $ this ->table ->createSelectionInstance ()
168184 ->wherePrimary ($ tmp + $ primary );
169185 }
@@ -205,8 +221,7 @@ public function delete(): int
205221 /** @return \ArrayIterator<string, mixed> */
206222 public function getIterator (): \Iterator
207223 {
208- $ this ->accessColumn (null );
209- return new \ArrayIterator ($ this ->data );
224+ return new \ArrayIterator ($ this ->toArray ());
210225 }
211226
212227
@@ -250,8 +265,10 @@ public function __set(string $column, mixed $value): void
250265 */
251266 public function &__get (string $ key ): mixed
252267 {
253- if ($ this ->accessColumn ($ key )) {
254- return $ this ->data [$ key ];
268+ $ column = $ this ->entityMapping ?->getColumnName($ key ) ?? $ key ;
269+
270+ if ($ this ->accessColumn ($ column )) {
271+ return $ this ->data [$ column ];
255272 }
256273
257274 $ referenced = $ this ->table ->getReferencedTable ($ this , $ key );
@@ -260,16 +277,21 @@ public function &__get(string $key): mixed
260277 return $ referenced ;
261278 }
262279
263- $ this ->removeAccessColumn ($ key );
264- $ hint = Nette \Utils \Helpers::getSuggestion (array_keys ($ this ->data ), $ key );
280+ $ this ->removeAccessColumn ($ column );
281+ $ available = $ this ->entityMapping
282+ ? array_map (fn (string $ col ) => $ this ->entityMapping ->getPropertyName ($ col ), array_keys ($ this ->data ))
283+ : array_keys ($ this ->data );
284+ $ hint = Nette \Utils \Helpers::getSuggestion ($ available , $ key );
265285 throw new Nette \MemberAccessException ("Cannot read an undeclared column ' $ key' " . ($ hint ? ", did you mean ' $ hint'? " : '. ' ));
266286 }
267287
268288
269289 public function __isset (string $ key ): bool
270290 {
271- if ($ this ->accessColumn ($ key )) {
272- return isset ($ this ->data [$ key ]);
291+ $ column = $ this ->entityMapping ?->getColumnName($ key ) ?? $ key ;
292+
293+ if ($ this ->accessColumn ($ column )) {
294+ return isset ($ this ->data [$ column ]);
273295 }
274296
275297 $ referenced = $ this ->table ->getReferencedTable ($ this , $ key );
@@ -278,7 +300,7 @@ public function __isset(string $key): bool
278300 return (bool ) $ referenced ;
279301 }
280302
281- $ this ->removeAccessColumn ($ key );
303+ $ this ->removeAccessColumn ($ column );
282304 return false ;
283305 }
284306
0 commit comments