Skip to content

Commit 00249c1

Browse files
committed
Fixes #236
1 parent c29d530 commit 00249c1

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function getTable()
222222
public function getAttribute($key)
223223
{
224224
// Check if the key is an array dot notation.
225-
if (strpos($key, '.') !== false)
225+
if (str_contains($key, '.'))
226226
{
227227
$attributes = array_dot($this->attributes);
228228

@@ -274,7 +274,7 @@ protected function getAttributeFromArray($key)
274274
return $this->attributes[$key];
275275
}
276276

277-
else if (strpos($key, '.') !== false)
277+
elseif (str_contains($key, '.'))
278278
{
279279
$attributes = array_dot($this->attributes);
280280

@@ -302,6 +302,17 @@ public function setAttribute($key, $value)
302302
$value = $builder->convertKey($value);
303303
}
304304

305+
// Support keys in dot notation.
306+
elseif (str_contains($key, '.'))
307+
{
308+
if (in_array($key, $this->getDates()) && $value)
309+
{
310+
$value = $this->fromDateTime($value);
311+
}
312+
313+
array_set($this->attributes, $key, $value); return;
314+
}
315+
305316
parent::setAttribute($key, $value);
306317
}
307318

src/Jenssegers/Mongodb/Relations/HasMany.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ public function getRelationCountQuery(Builder $query, Builder $parent)
2020
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
2121
}
2222

23+
/**
24+
* Get the plain foreign key.
25+
*
26+
* @return string
27+
*/
28+
public function getPlainForeignKey()
29+
{
30+
return $this->getForeignKey();
31+
}
32+
2333
}

src/Jenssegers/Mongodb/Relations/HasOne.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ public function getRelationCountQuery(Builder $query, Builder $parent)
2020
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
2121
}
2222

23+
/**
24+
* Get the plain foreign key.
25+
*
26+
* @return string
27+
*/
28+
public function getPlainForeignKey()
29+
{
30+
return $this->getForeignKey();
31+
}
32+
2333
}

tests/RelationsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public function testEasyRelation()
131131
$items = $user->items;
132132
$this->assertEquals(1, count($items));
133133
$this->assertInstanceOf('Item', $items[0]);
134+
$this->assertEquals($user->_id, $items[0]->user_id);
134135

135136
// Has one
136137
$user = User::create(array('name' => 'John Doe'));
@@ -141,6 +142,7 @@ public function testEasyRelation()
141142
$role = $user->role;
142143
$this->assertInstanceOf('Role', $role);
143144
$this->assertEquals('admin', $role->type);
145+
$this->assertEquals($user->_id, $role->user_id);
144146
}
145147

146148
public function testBelongsToMany()
@@ -415,6 +417,9 @@ public function testNestedKeys()
415417

416418
$address = $client->addresses->first();
417419
$this->assertEquals('Paris', $address->data['city']);
420+
421+
$client = Client::with('addresses')->first();
422+
$this->assertEquals('Paris', $client->addresses->first()->data['city']);
418423
}
419424

420425
public function testDoubleSaveOneToMany()

tests/models/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public function photo()
1919

2020
public function addresses()
2121
{
22-
return $this->hasMany('Address', 'data.client_id', 'data.address_id');
22+
return $this->hasMany('Address', 'data.address_id', 'data.client_id');
2323
}
2424
}

0 commit comments

Comments
 (0)