Commit 722668de authored by Jens Segers's avatar Jens Segers

Apply some Laravel changes

parent 64c46697
...@@ -38,6 +38,5 @@ ...@@ -38,6 +38,5 @@
"suggest": { "suggest": {
"jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB", "jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB",
"jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB" "jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB"
}, }
"minimum-stability": "dev"
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Model; use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\Relations\BelongsTo; use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany; use Jenssegers\Mongodb\Relations\BelongsToMany;
...@@ -135,10 +136,10 @@ trait HybridRelations { ...@@ -135,10 +136,10 @@ trait HybridRelations {
{ {
// If no relation name was given, we will use this debug backtrace to extract // If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most // the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships. // of the time this will be what we desire to use for the relationships.
if (is_null($relation)) if (is_null($relation))
{ {
list(, $caller) = debug_backtrace(false); list($current, $caller) = debug_backtrace(false, 2);
$relation = $caller['function']; $relation = $caller['function'];
} }
...@@ -154,7 +155,7 @@ trait HybridRelations { ...@@ -154,7 +155,7 @@ trait HybridRelations {
// when combined with an "_id" should conventionally match the columns. // when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey)) if (is_null($foreignKey))
{ {
$foreignKey = snake_case($relation) . '_id'; $foreignKey = Str::snake($relation) . '_id';
} }
$instance = new $related; $instance = new $related;
...@@ -184,9 +185,9 @@ trait HybridRelations { ...@@ -184,9 +185,9 @@ trait HybridRelations {
// use that to get both the class and foreign key that will be utilized. // use that to get both the class and foreign key that will be utilized.
if (is_null($name)) if (is_null($name))
{ {
list(, $caller) = debug_backtrace(false); list($current, $caller) = debug_backtrace(false, 2);
$name = snake_case($caller['function']); $name = Str::snake($caller['function']);
} }
list($type, $id) = $this->getMorphs($name, $type, $id); list($type, $id) = $this->getMorphs($name, $type, $id);
...@@ -201,15 +202,17 @@ trait HybridRelations { ...@@ -201,15 +202,17 @@ trait HybridRelations {
); );
} }
// If we are not eager loading the relatinship, we will essentially treat this // If we are not eager loading the relationship we will essentially treat this
// as a belongs-to style relationship since morph-to extends that class and // as a belongs-to style relationship since morph-to extends that class and
// we will pass in the appropriate values so that it behaves as expected. // we will pass in the appropriate values so that it behaves as expected.
else else
{ {
$class = $this->getActualClassNameForMorph($class);
$instance = new $class; $instance = new $class;
return new MorphTo( return new MorphTo(
with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
); );
} }
} }
......
...@@ -4,6 +4,7 @@ use Closure; ...@@ -4,6 +4,7 @@ use Closure;
use DateTime; use DateTime;
use Illuminate\Database\Query\Builder as BaseBuilder; use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression; use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Connection;
use MongoDate; use MongoDate;
...@@ -547,23 +548,24 @@ class Builder extends BaseBuilder { ...@@ -547,23 +548,24 @@ class Builder extends BaseBuilder {
} }
/** /**
* Pluck a single column from the database. * Get an array with the values of a given column.
* *
* @param string $column * @param string $column
* @return mixed * @param string|null $key
* @return array
*/ */
public function pluck($column) public function pluck($column, $key = null)
{ {
$result = (array) $this->first([$column]); $results = $this->get(is_null($key) ? [$column] : [$column, $key]);
// MongoDB returns the _id field even if you did not ask for it, so we need to // If the columns are qualified with a table or have an alias, we cannot use
// remove this from the result. // those directly in the "pluck" operations since the results from the DB
if (array_key_exists('_id', $result)) // are only keyed by the column itself. We'll strip the table out here.
{ return Arr::pluck(
unset($result['_id']); $results,
} $column,
$key
return count($result) > 0 ? reset($result) : null; );
} }
/** /**
......
<?php namespace Jenssegers\Mongodb\Relations; <?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo;
class MorphTo extends EloquentMorphTo { class MorphTo extends EloquentMorphTo {
......
...@@ -354,7 +354,7 @@ class QueryBuilderTest extends TestCase { ...@@ -354,7 +354,7 @@ class QueryBuilderTest extends TestCase {
]); ]);
$age = DB::collection('users')->where('name', 'John Doe')->pluck('age'); $age = DB::collection('users')->where('name', 'John Doe')->pluck('age');
$this->assertEquals(25, $age); $this->assertEquals([25], $age);
} }
public function testList() public function testList()
......
...@@ -364,11 +364,11 @@ class RelationsTest extends TestCase { ...@@ -364,11 +364,11 @@ class RelationsTest extends TestCase {
$photos = Photo::with('imageable')->get(); $photos = Photo::with('imageable')->get();
$relations = $photos[0]->getRelations(); $relations = $photos[0]->getRelations();
$this->assertTrue(array_key_exists('imageable', $relations)); $this->assertTrue(array_key_exists('imageable', $relations));
$this->assertInstanceOf('User', $relations['imageable']); $this->assertInstanceOf('User', $photos[0]->imageable);
$relations = $photos[1]->getRelations(); $relations = $photos[1]->getRelations();
$this->assertTrue(array_key_exists('imageable', $relations)); $this->assertTrue(array_key_exists('imageable', $relations));
$this->assertInstanceOf('Client', $relations['imageable']); $this->assertInstanceOf('Client', $photos[1]->imageable);
} }
public function testHasManyHas() public function testHasManyHas()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment