1
1
<?php namespace Jenssegers \Mongodb \Relations ;
2
2
3
- use Illuminate \Database \Eloquent \Collection ;
4
3
use Jenssegers \Mongodb \Model ;
4
+ use Illuminate \Database \Eloquent \Collection ;
5
5
use Illuminate \Database \Eloquent \Relations \BelongsToMany as EloquentBelongsToMany ;
6
6
7
7
class BelongsToMany extends EloquentBelongsToMany {
@@ -36,27 +36,25 @@ public function addConstraints()
36
36
{
37
37
if (static ::$ constraints )
38
38
{
39
- $ this ->query ->where ($ this ->foreignKey , $ this ->parent ->getKey ());
39
+ $ this ->query ->where ($ this ->getForeignKey (), ' = ' , $ this ->parent ->getKey ());
40
40
}
41
41
}
42
42
43
43
/**
44
- * Sync the intermediate tables with a list of IDs.
44
+ * Sync the intermediate tables with a list of IDs or collection of models .
45
45
*
46
46
* @param array $ids
47
47
* @param bool $detaching
48
48
* @return void
49
49
*/
50
50
public function sync (array $ ids , $ detaching = true )
51
51
{
52
+ if ($ ids instanceof Collection) $ ids = $ ids ->modelKeys ();
53
+
52
54
// First we need to attach any of the associated models that are not currently
53
55
// in this joining table. We'll spin through the given IDs, checking to see
54
56
// if they exist in the array of current ones, and if not we will insert.
55
- $ current = $ this ->parent ->{$ this ->otherKey };
56
-
57
- // Check if the current array exists or not on the parent model and create it
58
- // if it does not exist
59
- if (is_null ($ current )) $ current = array ();
57
+ $ current = $ this ->parent ->{$ this ->otherKey } ?: array ();
60
58
61
59
$ records = $ this ->formatSyncList ($ ids );
62
60
@@ -133,27 +131,6 @@ public function attach($id, array $attributes = array(), $touch = true)
133
131
if ($ touch ) $ this ->touchIfTouching ();
134
132
}
135
133
136
- /**
137
- * Create an array of records to insert into the pivot table.
138
- *
139
- * @param array $ids
140
- * @return void
141
- */
142
- protected function createAttachRecords ($ ids , array $ attributes )
143
- {
144
- $ records = array ();
145
-
146
- // To create the attachment records, we will simply spin through the IDs given
147
- // and create a new record to insert for each ID. Each ID may actually be a
148
- // key in the array, with extra attributes to be placed in other columns.
149
- foreach ($ ids as $ key => $ value )
150
- {
151
- $ records [] = $ this ->attacher ($ key , $ value , $ attributes , false );
152
- }
153
-
154
- return $ records ;
155
- }
156
-
157
134
/**
158
135
* Detach models from the relationship.
159
136
*
@@ -165,6 +142,8 @@ public function detach($ids = array(), $touch = true)
165
142
{
166
143
if ($ ids instanceof Model) $ ids = (array ) $ ids ->getKey ();
167
144
145
+ $ query = $ this ->getNewRelatedQuery ();
146
+
168
147
// If associated IDs were passed to the method we will only delete those
169
148
// associations, otherwise all of the association ties will be broken.
170
149
// We'll return the numbers of affected rows when we do the deletes.
@@ -176,9 +155,6 @@ public function detach($ids = array(), $touch = true)
176
155
$ this ->parent ->pull ($ this ->otherKey , $ id );
177
156
}
178
157
179
- // Get a new related query.
180
- $ query = $ this ->getNewRelatedQuery ();
181
-
182
158
// Prepare the query to select all related objects.
183
159
if (count ($ ids ) > 0 )
184
160
{
0 commit comments