Skip to content

Commit 722668d

Browse files
committed
Apply some Laravel changes
1 parent 64c4669 commit 722668d

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@
3838
"suggest": {
3939
"jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB",
4040
"jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB"
41-
},
42-
"minimum-stability": "dev"
41+
}
4342
}

src/Jenssegers/Mongodb/Eloquent/HybridRelations.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Eloquent\Relations\MorphMany;
44
use Illuminate\Database\Eloquent\Relations\MorphOne;
5+
use Illuminate\Support\Str;
56
use Jenssegers\Mongodb\Model;
67
use Jenssegers\Mongodb\Relations\BelongsTo;
78
use Jenssegers\Mongodb\Relations\BelongsToMany;
@@ -135,10 +136,10 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
135136
{
136137
// If no relation name was given, we will use this debug backtrace to extract
137138
// the calling method's name and use that as the relationship name as most
138-
// of the time this will be what we desire to use for the relatinoships.
139+
// of the time this will be what we desire to use for the relationships.
139140
if (is_null($relation))
140141
{
141-
list(, $caller) = debug_backtrace(false);
142+
list($current, $caller) = debug_backtrace(false, 2);
142143

143144
$relation = $caller['function'];
144145
}
@@ -154,7 +155,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
154155
// when combined with an "_id" should conventionally match the columns.
155156
if (is_null($foreignKey))
156157
{
157-
$foreignKey = snake_case($relation) . '_id';
158+
$foreignKey = Str::snake($relation) . '_id';
158159
}
159160

160161
$instance = new $related;
@@ -184,9 +185,9 @@ public function morphTo($name = null, $type = null, $id = null)
184185
// use that to get both the class and foreign key that will be utilized.
185186
if (is_null($name))
186187
{
187-
list(, $caller) = debug_backtrace(false);
188+
list($current, $caller) = debug_backtrace(false, 2);
188189

189-
$name = snake_case($caller['function']);
190+
$name = Str::snake($caller['function']);
190191
}
191192

192193
list($type, $id) = $this->getMorphs($name, $type, $id);
@@ -201,15 +202,17 @@ public function morphTo($name = null, $type = null, $id = null)
201202
);
202203
}
203204

204-
// If we are not eager loading the relatinship, we will essentially treat this
205+
// If we are not eager loading the relationship we will essentially treat this
205206
// as a belongs-to style relationship since morph-to extends that class and
206207
// we will pass in the appropriate values so that it behaves as expected.
207208
else
208209
{
210+
$class = $this->getActualClassNameForMorph($class);
211+
209212
$instance = new $class;
210213

211214
return new MorphTo(
212-
with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
215+
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
213216
);
214217
}
215218
}

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use DateTime;
55
use Illuminate\Database\Query\Builder as BaseBuilder;
66
use Illuminate\Database\Query\Expression;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\Collection;
89
use Jenssegers\Mongodb\Connection;
910
use MongoDate;
@@ -547,23 +548,24 @@ public function decrement($column, $amount = 1, array $extra = [], array $option
547548
}
548549

549550
/**
550-
* Pluck a single column from the database.
551+
* Get an array with the values of a given column.
551552
*
552553
* @param string $column
553-
* @return mixed
554+
* @param string|null $key
555+
* @return array
554556
*/
555-
public function pluck($column)
557+
public function pluck($column, $key = null)
556558
{
557-
$result = (array) $this->first([$column]);
558-
559-
// MongoDB returns the _id field even if you did not ask for it, so we need to
560-
// remove this from the result.
561-
if (array_key_exists('_id', $result))
562-
{
563-
unset($result['_id']);
564-
}
565-
566-
return count($result) > 0 ? reset($result) : null;
559+
$results = $this->get(is_null($key) ? [$column] : [$column, $key]);
560+
561+
// If the columns are qualified with a table or have an alias, we cannot use
562+
// those directly in the "pluck" operations since the results from the DB
563+
// are only keyed by the column itself. We'll strip the table out here.
564+
return Arr::pluck(
565+
$results,
566+
$column,
567+
$key
568+
);
567569
}
568570

569571
/**

src/Jenssegers/Mongodb/Relations/MorphTo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Jenssegers\Mongodb\Relations;
22

3+
use Illuminate\Database\Eloquent\Collection;
34
use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo;
45

56
class MorphTo extends EloquentMorphTo {

tests/QueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function testPluck()
354354
]);
355355

356356
$age = DB::collection('users')->where('name', 'John Doe')->pluck('age');
357-
$this->assertEquals(25, $age);
357+
$this->assertEquals([25], $age);
358358
}
359359

360360
public function testList()

tests/RelationsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ public function testMorph()
364364
$photos = Photo::with('imageable')->get();
365365
$relations = $photos[0]->getRelations();
366366
$this->assertTrue(array_key_exists('imageable', $relations));
367-
$this->assertInstanceOf('User', $relations['imageable']);
367+
$this->assertInstanceOf('User', $photos[0]->imageable);
368368

369369
$relations = $photos[1]->getRelations();
370370
$this->assertTrue(array_key_exists('imageable', $relations));
371-
$this->assertInstanceOf('Client', $relations['imageable']);
371+
$this->assertInstanceOf('Client', $photos[1]->imageable);
372372
}
373373

374374
public function testHasManyHas()

0 commit comments

Comments
 (0)