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
e3ff8801
Commit
e3ff8801
authored
Jun 18, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow dot notition for getAttribute
parent
57722d23
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
0 deletions
+61
-0
Model.php
src/Jenssegers/Mongodb/Model.php
+46
-0
ModelTest.php
tests/ModelTest.php
+15
-0
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
e3ff8801
...
@@ -195,6 +195,52 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -195,6 +195,52 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return
parent
::
getTable
();
return
parent
::
getTable
();
}
}
/**
* Get an attribute from the model.
*
* @param string $key
* @return mixed
*/
public
function
getAttribute
(
$key
)
{
// Check if the key is an array dot notation.
if
(
strstr
(
$key
,
'.'
))
{
$attributes
=
array_dot
(
$this
->
attributes
);
if
(
array_key_exists
(
$key
,
$attributes
))
{
return
$this
->
getAttributeValue
(
$key
);
}
}
return
parent
::
getAttribute
(
$key
);
}
/**
* Get an attribute from the $attributes array.
*
* @param string $key
* @return mixed
*/
protected
function
getAttributeFromArray
(
$key
)
{
if
(
array_key_exists
(
$key
,
$this
->
attributes
))
{
return
$this
->
attributes
[
$key
];
}
else
if
(
strstr
(
$key
,
'.'
))
{
$attributes
=
array_dot
(
$this
->
attributes
);
if
(
array_key_exists
(
$key
,
$attributes
))
{
return
$attributes
[
$key
];
}
}
}
/**
/**
* Set a given attribute on the model.
* Set a given attribute on the model.
*
*
...
...
tests/ModelTest.php
View file @
e3ff8801
...
@@ -419,4 +419,19 @@ class ModelTest extends TestCase {
...
@@ -419,4 +419,19 @@ class ModelTest extends TestCase {
$this
->
assertTrue
(
is_array
(
$result
));
$this
->
assertTrue
(
is_array
(
$result
));
}
}
public
function
testDotNotation
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'address'
=>
[
'city'
=>
'Paris'
,
'country'
=>
'France'
,
]
));
$this
->
assertEquals
(
'Paris'
,
$user
->
getAttribute
(
'address.city'
));
$this
->
assertEquals
(
'Paris'
,
$user
[
'address.city'
]);
$this
->
assertEquals
(
'Paris'
,
$user
->
{
'address.city'
});
}
}
}
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