2
2
3
3
use Illuminate \Database \Eloquent \Relations \HasOne ;
4
4
use Illuminate \Database \Eloquent \Relations \HasMany ;
5
+ use Illuminate \Database \Eloquent \Relations \MorphOne ;
6
+ use Illuminate \Database \Eloquent \Relations \MorphMany ;
5
7
use Jenssegers \Mongodb \Relations \BelongsTo ;
6
8
use Jenssegers \Mongodb \Relations \BelongsToMany ;
7
9
use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
@@ -33,6 +35,35 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
33
35
return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
34
36
}
35
37
38
+ /**
39
+ * Define a polymorphic one-to-one relationship.
40
+ *
41
+ * @param string $related
42
+ * @param string $name
43
+ * @param string $type
44
+ * @param string $id
45
+ * @param string $localKey
46
+ * @return \Illuminate\Database\Eloquent\Relations\MorphOne
47
+ */
48
+ public function morphOne ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
49
+ {
50
+ // Check if it is a relation with an original model.
51
+ if (!is_subclass_of ($ related , 'Jenssegers\Mongodb\Model ' ))
52
+ {
53
+ return parent ::morphOne ($ related , $ name , $ type , $ id , $ localKey );
54
+ }
55
+
56
+ $ instance = new $ related ;
57
+
58
+ list ($ type , $ id ) = $ this ->getMorphs ($ name , $ type , $ id );
59
+
60
+ $ table = $ instance ->getTable ();
61
+
62
+ $ localKey = $ localKey ?: $ this ->getKeyName ();
63
+
64
+ return new MorphOne ($ instance ->newQuery (), $ this , $ type , $ id , $ localKey );
65
+ }
66
+
36
67
/**
37
68
* Define a one-to-many relationship.
38
69
*
@@ -58,6 +89,38 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
58
89
return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
59
90
}
60
91
92
+ /**
93
+ * Define a polymorphic one-to-many relationship.
94
+ *
95
+ * @param string $related
96
+ * @param string $name
97
+ * @param string $type
98
+ * @param string $id
99
+ * @param string $localKey
100
+ * @return \Illuminate\Database\Eloquent\Relations\MorphMany
101
+ */
102
+ public function morphMany ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
103
+ {
104
+ // Check if it is a relation with an original model.
105
+ if (!is_subclass_of ($ related , 'Jenssegers\Mongodb\Model ' ))
106
+ {
107
+ return parent ::morphMany ($ related , $ name , $ type , $ id , $ localKey );
108
+ }
109
+
110
+ $ instance = new $ related ;
111
+
112
+ // Here we will gather up the morph type and ID for the relationship so that we
113
+ // can properly query the intermediate table of a relation. Finally, we will
114
+ // get the table and create the relationship instances for the developers.
115
+ list ($ type , $ id ) = $ this ->getMorphs ($ name , $ type , $ id );
116
+
117
+ $ table = $ instance ->getTable ();
118
+
119
+ $ localKey = $ localKey ?: $ this ->getKeyName ();
120
+
121
+ return new MorphMany ($ instance ->newQuery (), $ this , $ type , $ id , $ localKey );
122
+ }
123
+
61
124
/**
62
125
* Define an inverse one-to-one or many relationship.
63
126
*
0 commit comments