88
99trait BaseTrait
1010{
11- /**
12- * Preps the Schemas service with a current schema to share across this library and returns it.
13- *
14- * @return Schema
15- */
16- protected function _schema (): Schema
17- {
18- // Load the Schemas service
19- $ schemas = Services::schemas ();
20-
21- if (empty ($ schemas ))
22- {
23- throw new \RuntimeException (lang ('Relations.noSchemas ' ));
24- }
25-
26- // Check for a schema using the defaults
27- $ schema = $ schemas ->get ();
28-
29- if (is_null ($ schema ))
30- {
31- // Try reading an archived schema
32- $ schema = $ schemas ->read ()->get ();
33-
34- if (is_null ($ schema ))
35- {
36- // Give up
37- throw new \RuntimeException (lang ('Relations.noSchemas ' ));
38- }
39- }
40-
41- return $ schema ;
42- }
43-
44- /**
45- * Ensure this class has everything it needs to use Relations
46- */
47- protected function _isRelatable ()
48- {
49- if (empty ($ this ->table ))
50- {
51- throw RelationsException::forMissingProperty (get_class (), 'table ' );
52- }
53-
54- if (empty ($ this ->primaryKey ))
55- {
56- throw RelationsException::forMissingProperty (get_class (), 'primaryKey ' );
57- }
58-
59- // Make sure we have the inflector helper
60- if (! function_exists ('plural ' ))
61- {
62- helper ('inflector ' );
63- }
64- }
65-
6611 /**
6712 * Uses the schema to determine this class's relationship to a table
6813 *
@@ -72,6 +17,8 @@ protected function _isRelatable()
7217 */
7318 public function _getRelationship ($ tableName ): Relation
7419 {
20+ $ this ->_verifyRelatable ();
21+
7522 // Get the schema
7623 $ schema = $ this ->_schema ();
7724
@@ -112,6 +59,8 @@ public function _getRelationship($tableName): Relation
11259 */
11360 public function _getRelations ($ tableName , $ ids = null ): array
11461 {
62+ $ this ->_verifyRelatable ();
63+
11564 // Fetch the target table
11665 $ table = $ this ->_schema ()->tables ->{$ tableName };
11766
@@ -255,4 +204,61 @@ public function _getRelations($tableName, $ids = null): array
255204
256205 return $ return ;
257206 }
207+
208+ /**
209+ * Preps the Schemas service with a current schema to share across this library and returns it.
210+ *
211+ * @return Schema
212+ */
213+ protected function _schema (): Schema
214+ {
215+ $ this ->_verifyRelatable ();
216+
217+ // Load the Schemas service
218+ $ schemas = Services::schemas ();
219+
220+ if (empty ($ schemas ))
221+ {
222+ throw new \RuntimeException (lang ('Relations.noSchemas ' ));
223+ }
224+
225+ // Check for a schema using the defaults
226+ $ schema = $ schemas ->get ();
227+
228+ if (is_null ($ schema ))
229+ {
230+ // Try reading an archived schema
231+ $ schema = $ schemas ->read ()->get ();
232+
233+ if (is_null ($ schema ))
234+ {
235+ // Give up
236+ throw new \RuntimeException (lang ('Relations.noSchemas ' ));
237+ }
238+ }
239+
240+ return $ schema ;
241+ }
242+
243+ /**
244+ * Ensure this class has everything it needs to use Relations
245+ */
246+ protected function _verifyRelatable ()
247+ {
248+ if (empty ($ this ->table ))
249+ {
250+ throw RelationsException::forMissingProperty (get_class (), 'table ' );
251+ }
252+
253+ if (empty ($ this ->primaryKey ))
254+ {
255+ throw RelationsException::forMissingProperty (get_class (), 'primaryKey ' );
256+ }
257+
258+ // Make sure we have the inflector helper
259+ if (! function_exists ('plural ' ))
260+ {
261+ helper ('inflector ' );
262+ }
263+ }
258264}
0 commit comments