44namespace Cake \ElasticSearch \Association ;
55
66use Cake \Core \App ;
7+ use Cake \Datasource \FactoryLocator ;
78use Cake \ElasticSearch \Document ;
89use Cake \ElasticSearch \Exception \MissingDocumentException ;
910use Cake \ElasticSearch \Index ;
1011use Cake \Utility \Inflector ;
12+ use InvalidArgumentException ;
1113
1214/**
1315 * Represents an embedded document.
@@ -36,7 +38,7 @@ abstract class Embedded
3638 protected string $ alias ;
3739
3840 /**
39- * The class to use for the embeded document.
41+ * The class to use for the embedded document.
4042 */
4143 protected string $ entityClass ;
4244
@@ -50,6 +52,11 @@ abstract class Embedded
5052 */
5153 protected string $ indexClass ;
5254
55+ /**
56+ * Index instance this embed is linked to
57+ */
58+ protected ?Index $ index = null ;
59+
5360 /**
5461 * Constructor
5562 *
@@ -174,15 +181,30 @@ public function getIndexClass(): string
174181 /**
175182 * Set the index class used for this embed.
176183 *
177- * @param \Cake\ElasticSearch\Index|string|null $name The class name to set.
184+ * @param \Cake\ElasticSearch\Index|string|null $className The class name to set.
178185 * @return $this
186+ * @throws \InvalidArgumentException In case the class name is set after the target index has been
187+ * resolved, and it doesn't match the target index's class name.
179188 */
180- public function setIndexClass (string |Index |null $ name )
189+ public function setIndexClass (string |Index |null $ className )
181190 {
182- if ($ name instanceof Index) {
183- $ this ->indexClass = get_class ($ name );
184- } elseif (is_string ($ name )) {
185- $ class = App::className ($ name , 'Model/Index ' );
191+ if ($ className instanceof Index) {
192+ $ this ->index = $ className ;
193+ $ this ->indexClass = get_class ($ className );
194+ } elseif (is_string ($ className )) {
195+ $ class = App::className ($ className , 'Model/Index ' );
196+ if (
197+ $ class !== null &&
198+ $ this ->index instanceof Index &&
199+ get_class ($ this ->index ) !== $ class
200+ ) {
201+ throw new InvalidArgumentException (sprintf (
202+ "The class name `%s` doesn't match the target index class name of `%s`. " ,
203+ $ className ,
204+ $ this ->index ::class,
205+ ));
206+ }
207+
186208 if ($ class !== null ) {
187209 $ this ->indexClass = $ class ;
188210 }
@@ -191,6 +213,33 @@ public function setIndexClass(string|Index|null $name)
191213 return $ this ;
192214 }
193215
216+ /**
217+ * Sets the index instance for the target side of the association.
218+ *
219+ * @param \Cake\ElasticSearch\Index $index the instance to be assigned as target side
220+ * @return $this
221+ */
222+ public function setIndex (Index $ index )
223+ {
224+ $ this ->index = $ index ;
225+
226+ return $ this ;
227+ }
228+
229+ /**
230+ * Gets the index instance for the target side of the association.
231+ */
232+ public function getIndex (): Index
233+ {
234+ if (!$ this ->index instanceof Index) {
235+ /** @var \Cake\ElasticSearch\Index $index */
236+ $ index = FactoryLocator::get ('Elastic ' )->get ($ this ->getIndexClass ());
237+ $ this ->index = $ index ;
238+ }
239+
240+ return $ this ->index ;
241+ }
242+
194243 /**
195244 * Get the alias for this embed.
196245 */
0 commit comments