Skip to content

Commit 3fe1abb

Browse files
committed
Adding readme and tests for #248
1 parent 7330a6c commit 3fe1abb

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,17 @@ Again, you may override the conventional local key by passing a second argument
428428

429429
return $this->embedsMany('Book', 'local_key');
430430

431+
When using embedded documents, they will be stored in a _relation attribute of the parent document. This attribute is hidden by default when using `
432+
toArray` or `toJson`. If you want the attribute to be exposed, add it to `$exposed` property definition to your model:
433+
434+
use Jenssegers\Mongodb\Model as Eloquent;
435+
436+
class User extends Eloquent {
437+
438+
protected $exposed = array('_books');
439+
440+
}
441+
431442
### EmbedsOne Relations
432443

433444
There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model.

src/Jenssegers/Mongodb/Model.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
2929
protected $primaryKey = '_id';
3030

3131
/**
32-
* Allow json attributes to be exposable. This is mainly for
33-
* relations that can be kept alive in a toJson()
32+
* The attributes that should be exposed for toArray and toJson.
3433
*
3534
* @var array
3635
*/
37-
protected $expose = [];
36+
protected $exposed = array();
3837

3938
/**
4039
* The connection resolver instance.
@@ -292,7 +291,7 @@ public function attributesToArray()
292291
// internal array of embedded documents. In that case, we need
293292
// to hide these from the output so that the relation-based
294293
// attribute can take over.
295-
else if (starts_with($key, '_') and ! in_array($key, $this->expose))
294+
else if (starts_with($key, '_') and ! in_array($key, $this->exposed))
296295
{
297296
$camelKey = camel_case($key);
298297

@@ -316,7 +315,7 @@ public function attributesToArray()
316315
*/
317316
public function drop($columns)
318317
{
319-
if (!is_array($columns)) $columns = array($columns);
318+
if ( ! is_array($columns)) $columns = array($columns);
320319

321320
// Unset attributes
322321
foreach ($columns as $column)
@@ -357,6 +356,17 @@ public function pull()
357356
return call_user_func_array(array($query, 'pull'), func_get_args());
358357
}
359358

359+
/**
360+
* Set the exposed attributes for the model.
361+
*
362+
* @param array $exposed
363+
* @return void
364+
*/
365+
public function setExposed(array $exposed)
366+
{
367+
$this->exposed = $exposed;
368+
}
369+
360370
/**
361371
* Create a new Eloquent query builder for the model.
362372
*

tests/EmbeddedRelationsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,19 @@ public function testEmbedsOneDelete()
451451
$this->assertNull($user->father);
452452
}
453453

454+
public function testEmbedsManyToArray()
455+
{
456+
$user = User::create(array('name' => 'John Doe'));
457+
$user->addresses()->save(new Address(array('city' => 'New York')));
458+
$user->addresses()->save(new Address(array('city' => 'Paris')));
459+
$user->addresses()->save(new Address(array('city' => 'Brussels')));
460+
461+
$array = $user->toArray();
462+
$this->assertArrayNotHasKey('_addresses', $array);
463+
464+
$user->setExposed(array('_addresses'));
465+
$array = $user->toArray();
466+
$this->assertArrayHasKey('_addresses', $array);
467+
}
468+
454469
}

0 commit comments

Comments
 (0)