22
33use Illuminate \Database \Eloquent \Relations \HasOne ;
44use Illuminate \Database \Eloquent \Relations \HasMany ;
5+ use Illuminate \Database \Eloquent \Relations \MorphOne ;
6+ use Illuminate \Database \Eloquent \Relations \MorphMany ;
57use Jenssegers \Mongodb \Relations \BelongsTo ;
68use Jenssegers \Mongodb \Relations \BelongsToMany ;
79use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
@@ -33,6 +35,35 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
3335 return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
3436 }
3537
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+
3667 /**
3768 * Define a one-to-many relationship.
3869 *
@@ -58,6 +89,38 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
5889 return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
5990 }
6091
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+
61124 /**
62125 * Define an inverse one-to-one or many relationship.
63126 *
0 commit comments