Commit 257b92fe authored by Jens Segers's avatar Jens Segers

Updating model to 4.1 methods, check issue 63

parent 6e24848b
...@@ -114,66 +114,82 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -114,66 +114,82 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
} }
/** /**
* Define a one-to-one relationship. * Define a one-to-one relationship.
* *
* @param string $related * @param string $related
* @param string $foreignKey * @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne * @param string $localKey
*/ * @return \Illuminate\Database\Eloquent\Relations\HasOne
public function hasOne($related, $foreignKey = null) */
public function hasOne($related, $foreignKey = null, $localKey = null)
{ {
$foreignKey = $foreignKey ?: $this->getForeignKey(); $foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related; $instance = new $related;
return new HasOne($instance->newQuery(), $this, $foreignKey); $localKey = $localKey ?: $this->getKeyName();
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
} }
/** /**
* Define a one-to-many relationship. * Define a one-to-many relationship.
* *
* @param string $related * @param string $related
* @param string $foreignKey * @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @param string $localKey
*/ * @return \Illuminate\Database\Eloquent\Relations\HasMany
public function hasMany($related, $foreignKey = null) */
public function hasMany($related, $foreignKey = null, $localKey = null)
{ {
$foreignKey = $foreignKey ?: $this->getForeignKey(); $foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related; $instance = new $related;
return new HasMany($instance->newQuery(), $this, $foreignKey); $localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
} }
/** /**
* Define an inverse one-to-one or many relationship. * Define an inverse one-to-one or many relationship.
* *
* @param string $related * @param string $related
* @param string $foreignKey * @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @param string $otherKey
*/ * @param string $relation
public function belongsTo($related, $foreignKey = null) * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{ {
list(, $caller) = debug_backtrace(false); // 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.
if (is_null($relation))
{
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
// If no foreign key was supplied, we can use a backtrace to guess the proper // If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which // foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns. // when combined with an "_id" should conventionally match the columns.
$relation = $caller['function'];
if (is_null($foreignKey)) if (is_null($foreignKey))
{ {
$foreignKey = snake_case($relation).'_id'; $foreignKey = snake_case($relation).'_id';
} }
$instance = new $related;
// Once we have the foreign key names, we'll just create a new Eloquent query // Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will // for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations. // actually be responsible for retrieving and hydrating every relations.
$instance = new $related;
$query = $instance->newQuery(); $query = $instance->newQuery();
return new BelongsTo($query, $this, $foreignKey, $relation); $otherKey = $otherKey ?: $instance->getKeyName();
return new BelongsTo($query, $this, $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
...@@ -46,7 +46,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -46,7 +46,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(is_array($dbs)); $this->assertTrue(is_array($dbs));
} }
public function testMultipleConnections() /*public function testMultipleConnections()
{ {
global $app; global $app;
...@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$hosts = $mongoclient->getHosts(); $hosts = $mongoclient->getHosts();
$this->assertEquals(1, count($hosts)); $this->assertEquals(1, count($hosts));
} }*/
public function testQueryLog() public function testQueryLog()
{ {
......
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