2
2
3
3
namespace Simply \Database ;
4
4
5
+ use Simply \Database \Exception \InvalidRelationshipException ;
6
+
5
7
/**
6
8
* Record.
7
9
* @author Riikka Kalliomäki <[email protected] >
@@ -132,7 +134,7 @@ public function getReferencedRecords(string $name): array
132
134
$ name = $ this ->getSchema ()->getRelationship ($ name )->getName ();
133
135
134
136
if (!isset ($ this ->referencedRecords [$ name ])) {
135
- throw new \RuntimeException (" The referenced records for the relationship ' $ name ' have not been provided" );
137
+ throw new \RuntimeException (' The referenced records have not been provided ' );
136
138
}
137
139
138
140
return $ this ->referencedRecords [$ name ];
@@ -143,22 +145,22 @@ public function associate(string $name, Model $model): void
143
145
$ relationship = $ this ->getSchema ()->getRelationship ($ name );
144
146
145
147
if (!$ relationship ->isUniqueRelationship ()) {
146
- throw new \ InvalidArgumentException ('A single model can only be associated to an unique relationships ' );
148
+ throw new InvalidRelationshipException ('A single model can only be associated to an unique relationships ' );
147
149
}
148
150
149
151
$ keys = $ relationship ->getFields ();
150
152
$ fields = $ relationship ->getReferencedFields ();
151
153
$ record = $ model ->getDatabaseRecord ();
152
154
153
155
if ($ record ->getSchema () !== $ relationship ->getReferencedSchema ()) {
154
- throw new \InvalidArgumentException ('The associated model has a record with an unexpected schema ' );
156
+ throw new \InvalidArgumentException ('The associated record belongs to incorrect schema ' );
155
157
}
156
158
157
159
while ($ keys ) {
158
160
$ value = $ record [array_pop ($ fields )];
159
161
160
162
if ($ value === null ) {
161
- throw new \RuntimeException ('Cannot associate to models with nulls in referenced fields ' );
163
+ throw new \RuntimeException ('Cannot associate with models with nulls in referenced fields ' );
162
164
}
163
165
164
166
$ this [array_pop ($ keys )] = $ value ;
@@ -179,7 +181,7 @@ public function addAssociation(string $name, Model $model): void
179
181
$ relationship = $ this ->getSchema ()->getRelationship ($ name );
180
182
181
183
if ($ relationship ->isUniqueRelationship ()) {
182
- throw new \ InvalidArgumentException ('Cannot add a new model to an unique relationship ' );
184
+ throw new InvalidRelationshipException ('Cannot add a new model to an unique relationship ' );
183
185
}
184
186
185
187
$ model ->getDatabaseRecord ()->associate ($ relationship ->getReverseRelationship ()->getName (), $ this ->getModel ());
@@ -190,7 +192,7 @@ public function getRelatedModel(string $name): ?Model
190
192
$ relationship = $ this ->getSchema ()->getRelationship ($ name );
191
193
192
194
if (!$ relationship ->isUniqueRelationship ()) {
193
- throw new \ RuntimeException ('A single related model can only be fetched for an unique relationship ' );
195
+ throw new InvalidRelationshipException ('A single model can only be fetched for an unique relationship ' );
194
196
}
195
197
196
198
$ records = $ this ->getReferencedRecords ($ name );
@@ -207,7 +209,7 @@ public function getRelatedModels(string $name): array
207
209
$ relationship = $ this ->getSchema ()->getRelationship ($ name );
208
210
209
211
if ($ relationship ->isUniqueRelationship ()) {
210
- throw new \ RuntimeException ('Cannot fetch multiple models for an unique relationship ' );
212
+ throw new InvalidRelationshipException ('Cannot fetch multiple models for an unique relationship ' );
211
213
}
212
214
213
215
$ models = [];
@@ -225,11 +227,11 @@ public function getRelatedModelsByProxy(string $proxy, string $name): array
225
227
$ relationship = $ proxyRelationship ->getReferencedSchema ()->getRelationship ($ name );
226
228
227
229
if ($ proxyRelationship ->isUniqueRelationship ()) {
228
- throw new \ RuntimeException ('Cannot fetch related models via an unique proxy relationship ' );
230
+ throw new InvalidRelationshipException ('Cannot fetch models via an unique proxy relationship ' );
229
231
}
230
232
231
233
if (!$ relationship ->isUniqueRelationship ()) {
232
- throw new \ RuntimeException ( ' Related models can only be fetched via proxy with an unique relationship ' );
234
+ throw new InvalidRelationshipException ( ' Cannot fetch models via proxy without a unique relationship ' );
233
235
}
234
236
235
237
$ models = [];
0 commit comments