Skip to content

Commit b23b59e

Browse files
committed
Using underscore attribute for embedsMany, check #138
1 parent 128eb7b commit b23b59e

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ public function getIdAttribute($value)
5757
*/
5858
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
5959
{
60-
if (is_null($localKey))
61-
{
62-
$localKey = snake_case(str_plural($related)) . '_ids';
63-
}
64-
65-
if (is_null($foreignKey))
66-
{
67-
$foreignKey = snake_case(class_basename($this));
68-
}
69-
7060
// If no relation name was given, we will use this debug backtrace to extract
7161
// the calling method's name and use that as the relationship name as most
7262
// of the time this will be what we desire to use for the relatinoships.
@@ -77,6 +67,16 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
7767
$relation = $caller['function'];
7868
}
7969

70+
if (is_null($localKey))
71+
{
72+
$localKey = '_' . $relation;
73+
}
74+
75+
if (is_null($foreignKey))
76+
{
77+
$foreignKey = snake_case(class_basename($this));
78+
}
79+
8080
$query = $this->newQuery();
8181

8282
$instance = new $related;

tests/RelationsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public function testEmbedsManySave()
289289
$address = new Address(array('city' => 'London'));
290290

291291
$address = $user->addresses()->save($address);
292+
$this->assertNotNull($user->_addresses);
292293
$this->assertEquals(array('London'), $user->addresses->lists('city'));
293294
$this->assertInstanceOf('DateTime', $address->created_at);
294295
$this->assertInstanceOf('DateTime', $address->updated_at);

0 commit comments

Comments
 (0)