1313use Cake \ORM \Locator \LocatorAwareTrait ;
1414use Cake \Utility \Hash ;
1515use Cake \Utility \Inflector ;
16+ use RuntimeException ;
1617
1718class MappingStrategy
1819{
@@ -35,6 +36,13 @@ class MappingStrategy
3536 */
3637 protected array $ aliasList ;
3738
39+ /**
40+ * The map of aliases and corresponding Table objects.
41+ *
42+ * @var array<string,\Cake\ORM\Table|null>
43+ */
44+ protected array $ aliasMap = [];
45+
3846 /**
3947 * A list of aliases to be mapped.
4048 *
@@ -63,12 +71,14 @@ public function __construct(Table $rootTable, array $aliases)
6371 }
6472 $ this ->aliasList = $ aliases ;
6573 $ this ->unknownAliases = array_combine ($ aliases , $ aliases );
74+ $ this ->aliasMap = array_fill_keys ($ aliases , null );
6675 $ rootAlias = $ rootTable ->getAlias ();
6776 if (!isset ($ this ->unknownAliases [$ rootAlias ])) {
6877 $ message = "The query must select at least one column from the root table. " ;
6978 $ message .= " The column alias must use {$ rootAlias }__{column_name} format " ;
7079 throw new UnknownAliasException ($ message );
7180 }
81+ $ this ->aliasMap [$ rootAlias ] = $ rootTable ;
7282 unset($ this ->unknownAliases [$ rootAlias ]);
7383 }
7484
@@ -120,6 +130,7 @@ private function scanRootLevel(Table $table): array
120130 if (!isset ($ this ->unknownAliases [$ alias ])) {
121131 continue ;
122132 }
133+ $ this ->aliasMap [$ alias ] = $ target ;
123134 unset($ this ->unknownAliases [$ alias ]);
124135 $ firstLevelAssoc = [
125136 'className ' => $ target ->getEntityClass (),
@@ -137,6 +148,7 @@ private function scanRootLevel(Table $table): array
137148 'primaryKey ' => $ assoc ->junction ()->getPrimaryKey (),
138149 'propertyName ' => Inflector::underscore (Inflector::singularize ($ through )),
139150 ];
151+ $ this ->aliasMap [$ through ] = $ assoc ->junction ();
140152 unset($ this ->unknownAliases [$ through ]);
141153 }
142154 }
@@ -179,6 +191,7 @@ private function scanTableRecursive(string $alias): array
179191 if (!isset ($ this ->unknownAliases [$ childAlias ])) {
180192 continue ;
181193 }
194+ $ this ->aliasMap [$ childAlias ] = $ target ;
182195 unset($ this ->unknownAliases [$ childAlias ]);
183196 $ result [$ type ][$ childAlias ]['className ' ] = $ target ->getEntityClass ();
184197 $ result [$ type ][$ childAlias ]['primaryKey ' ] = $ target ->getPrimaryKey ();
@@ -194,6 +207,7 @@ private function scanTableRecursive(string $alias): array
194207 'propertyName ' => Inflector::underscore (Inflector::singularize ($ through )),
195208 ];
196209 if (isset ($ this ->unknownAliases [$ through ])) {
210+ $ this ->aliasMap [$ through ] = $ assoc ->junction ();
197211 unset($ this ->unknownAliases [$ through ]);
198212 }
199213 } else {
@@ -241,4 +255,18 @@ private function unknownAliasesToString(): string
241255 {
242256 return implode ("', ' " , array_keys ($ this ->unknownAliases ));
243257 }
258+
259+ /**
260+ * Gets aliases map.
261+ *
262+ * @return array<string,\Cake\ORM\Table> Keys are alias names.
263+ */
264+ public function getAliasMap (): array
265+ {
266+ $ aliasWithoutTable = array_search (null , $ this ->aliasMap , true );
267+ if (in_array (null , $ this ->aliasMap , true ) || $ aliasWithoutTable !== false ) {
268+ throw new RuntimeException ("Failed to locate Table object for alias ' $ aliasWithoutTable' " );
269+ }
270+ return $ this ->aliasMap ;
271+ }
244272}
0 commit comments