Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
3fe1abb7
Commit
3fe1abb7
authored
Jul 10, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding readme and tests for #248
parent
7330a6c0
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
5 deletions
+41
-5
README.md
README.md
+11
-0
Model.php
src/Jenssegers/Mongodb/Model.php
+15
-5
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+15
-0
No files found.
README.md
View file @
3fe1abb7
...
...
@@ -428,6 +428,17 @@ Again, you may override the conventional local key by passing a second argument
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
There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model.
...
...
src/Jenssegers/Mongodb/Model.php
View file @
3fe1abb7
...
...
@@ -29,12 +29,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
protected
$primaryKey
=
'_id'
;
/**
* Allow json attributes to be exposable. This is mainly for
* relations that can be kept alive in a toJson()
* The attributes that should be exposed for toArray and toJson.
*
* @var array
*/
protected
$expose
=
[]
;
protected
$expose
d
=
array
()
;
/**
* The connection resolver instance.
...
...
@@ -292,7 +291,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
// internal array of embedded documents. In that case, we need
// to hide these from the output so that the relation-based
// 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
->
expose
d
))
{
$camelKey
=
camel_case
(
$key
);
...
...
@@ -316,7 +315,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
public
function
drop
(
$columns
)
{
if
(
!
is_array
(
$columns
))
$columns
=
array
(
$columns
);
if
(
!
is_array
(
$columns
))
$columns
=
array
(
$columns
);
// Unset attributes
foreach
(
$columns
as
$column
)
...
...
@@ -357,6 +356,17 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
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.
*
...
...
tests/EmbeddedRelationsTest.php
View file @
3fe1abb7
...
...
@@ -451,4 +451,19 @@ class EmbeddedRelationsTest extends TestCase {
$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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment