Skip to content

Commit b9e4cda

Browse files
committed
Tweaking relations and MongoId conversion
1 parent 9443e8b commit b9e4cda

File tree

9 files changed

+276
-396
lines changed

9 files changed

+276
-396
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,27 +169,23 @@ public function getTable()
169169
}
170170

171171
/**
172-
* Set the array of model attributes. No checking is done.
172+
* Get an attribute from the model.
173173
*
174-
* @param array $attributes
175-
* @param bool $sync
176-
* @return void
174+
* @param string $key
175+
* @return mixed
177176
*/
178-
public function setRawAttributes(array $attributes, $sync = false)
177+
public function getAttribute($key)
179178
{
180-
foreach($attributes as $key => &$value)
179+
$attribute = parent::getAttribute($key);
180+
181+
// If the attribute is a MongoId object, return it as a string.
182+
// This is makes Eloquent relations a lot easier.
183+
if ($attribute instanceof MongoId)
181184
{
182-
/**
183-
* MongoIds are converted to string to make it easier to pass
184-
* the id to other instances or relations.
185-
*/
186-
if ($value instanceof MongoId)
187-
{
188-
$value = (string) $value;
189-
}
185+
return (string) $attribute;
190186
}
191187

192-
parent::setRawAttributes($attributes, $sync);
188+
return $attribute;
193189
}
194190

195191
/**
@@ -223,7 +219,7 @@ public function attributesToArray()
223219
* @param mixed $columns
224220
* @return int
225221
*/
226-
public function dropColumn($columns)
222+
public function drop($columns)
227223
{
228224
if (!is_array($columns)) $columns = array($columns);
229225

@@ -234,7 +230,7 @@ public function dropColumn($columns)
234230
}
235231

236232
// Perform unset only on current document
237-
return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
233+
return $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
238234
}
239235

240236
/**
@@ -289,7 +285,7 @@ public function __call($method, $parameters)
289285
// Unset method
290286
if ($method == 'unset')
291287
{
292-
return call_user_func_array(array($this, 'dropColumn'), $parameters);
288+
return call_user_func_array(array($this, 'drop'), $parameters);
293289
}
294290

295291
return parent::__call($method, $parameters);

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(Connection $connection)
5959
/**
6060
* Execute a query for a single record by ID.
6161
*
62-
* @param int $id
62+
* @param mixed $id
6363
* @param array $columns
6464
* @return mixed
6565
*/
@@ -377,8 +377,8 @@ public function insertGetId(array $values, $sequence = null)
377377
$sequence = '_id';
378378
}
379379

380-
// Return id as a string
381-
return (string) $values[$sequence];
380+
// Return id
381+
return $values[$sequence];
382382
}
383383
}
384384

@@ -585,7 +585,7 @@ public function pull($column, $value = null)
585585
* @param mixed $columns
586586
* @return int
587587
*/
588-
public function dropColumn($columns)
588+
public function drop($columns)
589589
{
590590
if (!is_array($columns)) $columns = array($columns);
591591

@@ -846,7 +846,7 @@ public function __call($method, $parameters)
846846
{
847847
if ($method == 'unset')
848848
{
849-
return call_user_func_array(array($this, 'dropColumn'), $parameters);
849+
return call_user_func_array(array($this, 'drop'), $parameters);
850850
}
851851

852852
return parent::__call($method, $parameters);

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace Jenssegers\Mongodb\Relations;
22

3-
use Illuminate\Database\Eloquent\Collection;
43
use Jenssegers\Mongodb\Model;
4+
use Illuminate\Database\Eloquent\Collection;
55
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
66

77
class 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

Comments
 (0)