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

Apply some Laravel changes

parent 64c46697
......@@ -38,6 +38,5 @@
"suggest": {
"jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB",
"jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB"
},
"minimum-stability": "dev"
}
}
......@@ -2,6 +2,7 @@
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany;
......@@ -135,10 +136,10 @@ trait HybridRelations {
{
// 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
// 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))
{
list(, $caller) = debug_backtrace(false);
list($current, $caller) = debug_backtrace(false, 2);
$relation = $caller['function'];
}
......@@ -154,7 +155,7 @@ trait HybridRelations {
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey))
{
$foreignKey = snake_case($relation) . '_id';
$foreignKey = Str::snake($relation) . '_id';
}
$instance = new $related;
......@@ -184,9 +185,9 @@ trait HybridRelations {
// use that to get both the class and foreign key that will be utilized.
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);
......@@ -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
// we will pass in the appropriate values so that it behaves as expected.
else
{
$class = $this->getActualClassNameForMorph($class);
$instance = new $class;
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;
use DateTime;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Connection;
use MongoDate;
......@@ -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
* @return mixed
* @param string|null $key
* @return array
*/
public function pluck($column)
public function pluck($column, $key = null)
{
$result = (array) $this->first([$column]);
// MongoDB returns the _id field even if you did not ask for it, so we need to
// remove this from the result.
if (array_key_exists('_id', $result))
{
unset($result['_id']);
}
return count($result) > 0 ? reset($result) : null;
$results = $this->get(is_null($key) ? [$column] : [$column, $key]);
// If the columns are qualified with a table or have an alias, we cannot use
// those directly in the "pluck" operations since the results from the DB
// are only keyed by the column itself. We'll strip the table out here.
return Arr::pluck(
$results,
$column,
$key
);
}
/**
......
<?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo;
class MorphTo extends EloquentMorphTo {
......
......@@ -354,7 +354,7 @@ class QueryBuilderTest extends TestCase {
]);
$age = DB::collection('users')->where('name', 'John Doe')->pluck('age');
$this->assertEquals(25, $age);
$this->assertEquals([25], $age);
}
public function testList()
......
......@@ -364,11 +364,11 @@ class RelationsTest extends TestCase {
$photos = Photo::with('imageable')->get();
$relations = $photos[0]->getRelations();
$this->assertTrue(array_key_exists('imageable', $relations));
$this->assertInstanceOf('User', $relations['imageable']);
$this->assertInstanceOf('User', $photos[0]->imageable);
$relations = $photos[1]->getRelations();
$this->assertTrue(array_key_exists('imageable', $relations));
$this->assertInstanceOf('Client', $relations['imageable']);
$this->assertInstanceOf('Client', $photos[1]->imageable);
}
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