Commit 3fe1abb7 authored by Jens Segers's avatar Jens Segers

Adding readme and tests for #248

parent 7330a6c0
...@@ -428,6 +428,17 @@ Again, you may override the conventional local key by passing a second argument ...@@ -428,6 +428,17 @@ Again, you may override the conventional local key by passing a second argument
return $this->embedsMany('Book', 'local_key'); return $this->embedsMany('Book', 'local_key');
When using embedded documents, they will be stored in a _relation attribute of the parent document. This attribute is hidden by default when using `
toArray` or `toJson`. If you want the attribute to be exposed, add it to `$exposed` property definition to your model:
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent {
protected $exposed = array('_books');
}
### EmbedsOne Relations ### EmbedsOne Relations
There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model. There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model.
......
...@@ -29,12 +29,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -29,12 +29,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
protected $primaryKey = '_id'; protected $primaryKey = '_id';
/** /**
* Allow json attributes to be exposable. This is mainly for * The attributes that should be exposed for toArray and toJson.
* relations that can be kept alive in a toJson()
* *
* @var array * @var array
*/ */
protected $expose = []; protected $exposed = array();
/** /**
* The connection resolver instance. * The connection resolver instance.
...@@ -292,7 +291,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -292,7 +291,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
// internal array of embedded documents. In that case, we need // internal array of embedded documents. In that case, we need
// to hide these from the output so that the relation-based // to hide these from the output so that the relation-based
// attribute can take over. // attribute can take over.
else if (starts_with($key, '_') and ! in_array($key, $this->expose)) else if (starts_with($key, '_') and ! in_array($key, $this->exposed))
{ {
$camelKey = camel_case($key); $camelKey = camel_case($key);
...@@ -316,7 +315,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -316,7 +315,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/ */
public function drop($columns) public function drop($columns)
{ {
if (!is_array($columns)) $columns = array($columns); if ( ! is_array($columns)) $columns = array($columns);
// Unset attributes // Unset attributes
foreach ($columns as $column) foreach ($columns as $column)
...@@ -357,6 +356,17 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -357,6 +356,17 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return call_user_func_array(array($query, 'pull'), func_get_args()); return call_user_func_array(array($query, 'pull'), func_get_args());
} }
/**
* Set the exposed attributes for the model.
*
* @param array $exposed
* @return void
*/
public function setExposed(array $exposed)
{
$this->exposed = $exposed;
}
/** /**
* Create a new Eloquent query builder for the model. * Create a new Eloquent query builder for the model.
* *
......
...@@ -451,4 +451,19 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -451,4 +451,19 @@ class EmbeddedRelationsTest extends TestCase {
$this->assertNull($user->father); $this->assertNull($user->father);
} }
public function testEmbedsManyToArray()
{
$user = User::create(array('name' => 'John Doe'));
$user->addresses()->save(new Address(array('city' => 'New York')));
$user->addresses()->save(new Address(array('city' => 'Paris')));
$user->addresses()->save(new Address(array('city' => 'Brussels')));
$array = $user->toArray();
$this->assertArrayNotHasKey('_addresses', $array);
$user->setExposed(array('_addresses'));
$array = $user->toArray();
$this->assertArrayHasKey('_addresses', $array);
}
} }
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