Commit e6f1a579 authored by Jens Segers's avatar Jens Segers

Merge branch 'develop'

parents aa017699 51b32340
...@@ -11,13 +11,13 @@ ...@@ -11,13 +11,13 @@
"license" : "MIT", "license" : "MIT",
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.0",
"illuminate/support": "4.0.x", "illuminate/support": "4.1.x",
"illuminate/database": "4.0.x", "illuminate/database": "4.1.x",
"illuminate/events": "4.0.x" "illuminate/events": "4.1.x"
}, },
"require-dev": { "require-dev": {
"illuminate/cache": "4.0.x", "illuminate/cache": "4.1.x",
"illuminate/auth": "4.0.x" "illuminate/auth": "4.1.x"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {
......
...@@ -204,14 +204,23 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -204,14 +204,23 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
* Define a many-to-many relationship. * Define a many-to-many relationship.
* *
* @param string $related * @param string $related
* @param string $table * @param string $collection
* @param string $foreignKey * @param string $foreignKey
* @param string $otherKey * @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/ */
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null) public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
{ {
$caller = $this->getBelongsToManyCaller(); // If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
if (is_null($relation))
{
$caller = $this->getBelongsToManyCaller();
$name = $caller['function'];
}
// First, we'll need to determine the foreign key and "other key" for the // First, we'll need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we'll make the query // relationship. Once we have determined the keys we'll make the query
...@@ -235,7 +244,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -235,7 +244,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
// appropriate query constraint and entirely manages the hydrations. // appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery(); $query = $instance->newQuery();
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $caller['function']); return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
} }
/** /**
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo { class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
/** /**
* Set the base constraints on the relation query. * Set the base constraints on the relation query.
* *
* @return void * @return void
*/ */
public function addConstraints() public function addConstraints()
{ {
if (static::$constraints) if (static::$constraints)
...@@ -14,9 +14,9 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo { ...@@ -14,9 +14,9 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
// For belongs to relationships, which are essentially the inverse of has one // For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key // or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent. // of the related models matching on the foreign key that's on a parent.
$key = $this->related->getKeyName(); $table = $this->related->getTable();
$this->query->where($key, '=', $this->parent->{$this->foreignKey}); $this->query->where($this->otherKey, '=', $this->parent->{$this->foreignKey});
} }
} }
...@@ -31,9 +31,9 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo { ...@@ -31,9 +31,9 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
// We'll grab the primary key name of the related models since it could be set to // We'll grab the primary key name of the related models since it could be set to
// a non-standard name and not "id". We will then construct the constraint for // a non-standard name and not "id". We will then construct the constraint for
// our eagerly loading query so it returns the proper models from execution. // our eagerly loading query so it returns the proper models from execution.
$key = $this->related->getKeyName(); $key = $this->otherKey;
$this->query->whereIn($key, $this->getEagerModelKeys($models)); $this->query->whereIn($key, $this->getEagerModelKeys($models));
} }
} }
\ No newline at end of file
...@@ -265,10 +265,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -265,10 +265,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
)); ));
$items = DB::collection('items')->distinct('name')->get(); $items = DB::collection('items')->distinct('name')->get();
$this->assertEquals(array('knife', 'fork', 'spoon'), $items); $this->assertEquals(3, count($items));
$types = DB::collection('items')->distinct('type')->get(); $types = DB::collection('items')->distinct('type')->get();
$this->assertEquals(array('sharp', 'round'), $types); $this->assertEquals(2, count($types));
} }
public function testCustomId() public function testCustomId()
......
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