11<?php namespace Jenssegers \Mongodb \Relations ;
22
3- use Jenssegers \ Mongodb \Model ;
3+ use Illuminate \ Database \ Eloquent \Model ;
44use Illuminate \Database \Eloquent \Collection ;
55use Illuminate \Database \Eloquent \Relations \BelongsToMany as EloquentBelongsToMany ;
66
@@ -40,6 +40,45 @@ public function addConstraints()
4040 }
4141 }
4242
43+ /**
44+ * Save a new model and attach it to the parent model.
45+ *
46+ * @param \Illuminate\Database\Eloquent\Model $model
47+ * @param array $joining
48+ * @param bool $touch
49+ * @return \Illuminate\Database\Eloquent\Model
50+ */
51+ public function save (Model $ model , array $ joining = array (), $ touch = true )
52+ {
53+ $ model ->save (array ('touch ' => false ));
54+
55+ $ this ->attach ($ model , $ joining , $ touch );
56+
57+ return $ model ;
58+ }
59+
60+ /**
61+ * Create a new instance of the related model.
62+ *
63+ * @param array $attributes
64+ * @param array $joining
65+ * @param bool $touch
66+ * @return \Illuminate\Database\Eloquent\Model
67+ */
68+ public function create (array $ attributes , array $ joining = array (), $ touch = true )
69+ {
70+ $ instance = $ this ->related ->newInstance ($ attributes );
71+
72+ // Once we save the related model, we need to attach it to the base model via
73+ // through intermediate table so we'll use the existing "attach" method to
74+ // accomplish this which will insert the record and any more attributes.
75+ $ instance ->save (array ('touch ' => false ));
76+
77+ $ this ->attach ($ instance , $ joining , $ touch );
78+
79+ return $ instance ;
80+ }
81+
4382 /**
4483 * Sync the intermediate tables with a list of IDs or collection of models.
4584 *
@@ -102,7 +141,7 @@ public function sync($ids, $detaching = true)
102141 */
103142 public function updateExistingPivot ($ id , array $ attributes , $ touch = true )
104143 {
105- // TODO
144+ // Do nothing, we have no pivot table.
106145 }
107146
108147 /**
@@ -115,7 +154,10 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
115154 */
116155 public function attach ($ id , array $ attributes = array (), $ touch = true )
117156 {
118- if ($ id instanceof Model) $ id = $ id ->getKey ();
157+ if ($ id instanceof Model)
158+ {
159+ $ model = $ id ; $ id = $ model ->getKey ();
160+ }
119161
120162 $ records = $ this ->createAttachRecords ((array ) $ id , $ attributes );
121163
@@ -126,14 +168,23 @@ public function attach($id, array $attributes = array(), $touch = true)
126168 // Attach the new ids to the parent model.
127169 $ this ->parent ->push ($ this ->otherKey , $ otherIds , true );
128170
129- // Generate a new related query instance.
130- $ query = $ this ->newRelatedQuery ();
171+ // If we have a model instance, we can psuh the ids to that model,
172+ // so that the internal attributes are updated as well. Otherwise,
173+ // we will just perform a regular database query.
174+ if (isset ($ model ))
175+ {
176+ // Attach the new ids to the related model.
177+ $ model ->push ($ this ->foreignKey , $ foreignIds , true );
178+ }
179+ else
180+ {
181+ $ query = $ this ->newRelatedQuery ();
131182
132- // Set contraints on the related query.
133- $ query ->where ($ this ->related ->getKeyName (), $ id );
183+ $ query ->where ($ this ->related ->getKeyName (), $ id );
134184
135- // Attach the new ids to the related model.
136- $ query ->push ($ this ->foreignKey , $ foreignIds , true );
185+ // Attach the new ids to the related model.
186+ $ query ->push ($ this ->foreignKey , $ foreignIds , true );
187+ }
137188
138189 if ($ touch ) $ this ->touchIfTouching ();
139190 }
0 commit comments