Commit 00249c1f authored by Jens Segers's avatar Jens Segers

Fixes #236

parent c29d5301
...@@ -222,7 +222,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -222,7 +222,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
public function getAttribute($key) public function getAttribute($key)
{ {
// Check if the key is an array dot notation. // Check if the key is an array dot notation.
if (strpos($key, '.') !== false) if (str_contains($key, '.'))
{ {
$attributes = array_dot($this->attributes); $attributes = array_dot($this->attributes);
...@@ -274,7 +274,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -274,7 +274,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return $this->attributes[$key]; return $this->attributes[$key];
} }
else if (strpos($key, '.') !== false) elseif (str_contains($key, '.'))
{ {
$attributes = array_dot($this->attributes); $attributes = array_dot($this->attributes);
...@@ -302,6 +302,17 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -302,6 +302,17 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
$value = $builder->convertKey($value); $value = $builder->convertKey($value);
} }
// Support keys in dot notation.
elseif (str_contains($key, '.'))
{
if (in_array($key, $this->getDates()) && $value)
{
$value = $this->fromDateTime($value);
}
array_set($this->attributes, $key, $value); return;
}
parent::setAttribute($key, $value); parent::setAttribute($key, $value);
} }
......
...@@ -20,4 +20,14 @@ class HasMany extends EloquentHasMany { ...@@ -20,4 +20,14 @@ class HasMany extends EloquentHasMany {
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true); return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
} }
/**
* Get the plain foreign key.
*
* @return string
*/
public function getPlainForeignKey()
{
return $this->getForeignKey();
}
} }
...@@ -20,4 +20,14 @@ class HasOne extends EloquentHasOne { ...@@ -20,4 +20,14 @@ class HasOne extends EloquentHasOne {
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true); return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
} }
/**
* Get the plain foreign key.
*
* @return string
*/
public function getPlainForeignKey()
{
return $this->getForeignKey();
}
} }
...@@ -131,6 +131,7 @@ class RelationsTest extends TestCase { ...@@ -131,6 +131,7 @@ class RelationsTest extends TestCase {
$items = $user->items; $items = $user->items;
$this->assertEquals(1, count($items)); $this->assertEquals(1, count($items));
$this->assertInstanceOf('Item', $items[0]); $this->assertInstanceOf('Item', $items[0]);
$this->assertEquals($user->_id, $items[0]->user_id);
// Has one // Has one
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
...@@ -141,6 +142,7 @@ class RelationsTest extends TestCase { ...@@ -141,6 +142,7 @@ class RelationsTest extends TestCase {
$role = $user->role; $role = $user->role;
$this->assertInstanceOf('Role', $role); $this->assertInstanceOf('Role', $role);
$this->assertEquals('admin', $role->type); $this->assertEquals('admin', $role->type);
$this->assertEquals($user->_id, $role->user_id);
} }
public function testBelongsToMany() public function testBelongsToMany()
...@@ -415,6 +417,9 @@ class RelationsTest extends TestCase { ...@@ -415,6 +417,9 @@ class RelationsTest extends TestCase {
$address = $client->addresses->first(); $address = $client->addresses->first();
$this->assertEquals('Paris', $address->data['city']); $this->assertEquals('Paris', $address->data['city']);
$client = Client::with('addresses')->first();
$this->assertEquals('Paris', $client->addresses->first()->data['city']);
} }
public function testDoubleSaveOneToMany() public function testDoubleSaveOneToMany()
......
...@@ -19,6 +19,6 @@ class Client extends Eloquent { ...@@ -19,6 +19,6 @@ class Client extends Eloquent {
public function addresses() public function addresses()
{ {
return $this->hasMany('Address', 'data.client_id', 'data.address_id'); return $this->hasMany('Address', 'data.address_id', 'data.client_id');
} }
} }
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