11<?php namespace Jenssegers \Mongodb \Relations ;
22
3- use Illuminate \Database \Eloquent \Collection ;
43use Jenssegers \Mongodb \Model ;
4+ use Illuminate \Database \Eloquent \Collection ;
55use Illuminate \Database \Eloquent \Relations \BelongsToMany as EloquentBelongsToMany ;
66
77class BelongsToMany extends EloquentBelongsToMany {
@@ -36,27 +36,25 @@ public function addConstraints()
3636 {
3737 if (static ::$ constraints )
3838 {
39- $ this ->query ->where ($ this ->foreignKey , $ this ->parent ->getKey ());
39+ $ this ->query ->where ($ this ->getForeignKey (), ' = ' , $ this ->parent ->getKey ());
4040 }
4141 }
4242
4343 /**
44- * Sync the intermediate tables with a list of IDs.
44+ * Sync the intermediate tables with a list of IDs or collection of models .
4545 *
4646 * @param array $ids
4747 * @param bool $detaching
4848 * @return void
4949 */
5050 public function sync (array $ ids , $ detaching = true )
5151 {
52+ if ($ ids instanceof Collection) $ ids = $ ids ->modelKeys ();
53+
5254 // First we need to attach any of the associated models that are not currently
5355 // in this joining table. We'll spin through the given IDs, checking to see
5456 // 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 ();
6058
6159 $ records = $ this ->formatSyncList ($ ids );
6260
@@ -133,27 +131,6 @@ public function attach($id, array $attributes = array(), $touch = true)
133131 if ($ touch ) $ this ->touchIfTouching ();
134132 }
135133
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-
157134 /**
158135 * Detach models from the relationship.
159136 *
@@ -165,6 +142,8 @@ public function detach($ids = array(), $touch = true)
165142 {
166143 if ($ ids instanceof Model) $ ids = (array ) $ ids ->getKey ();
167144
145+ $ query = $ this ->getNewRelatedQuery ();
146+
168147 // If associated IDs were passed to the method we will only delete those
169148 // associations, otherwise all of the association ties will be broken.
170149 // We'll return the numbers of affected rows when we do the deletes.
@@ -176,9 +155,6 @@ public function detach($ids = array(), $touch = true)
176155 $ this ->parent ->pull ($ this ->otherKey , $ id );
177156 }
178157
179- // Get a new related query.
180- $ query = $ this ->getNewRelatedQuery ();
181-
182158 // Prepare the query to select all related objects.
183159 if (count ($ ids ) > 0 )
184160 {
0 commit comments