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
03cee24c
Commit
03cee24c
authored
Aug 22, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow custom id's, fixes #24
parent
18a1010c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
3 deletions
+34
-3
Builder.php
src/Jenssegers/Mongodb/Builder.php
+19
-3
QueryTest.php
tests/QueryTest.php
+15
-0
No files found.
src/Jenssegers/Mongodb/Builder.php
View file @
03cee24c
...
@@ -48,7 +48,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -48,7 +48,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
find
(
$id
,
$columns
=
array
(
'*'
))
public
function
find
(
$id
,
$columns
=
array
(
'*'
))
{
{
return
$this
->
where
(
'_id'
,
'='
,
new
MongoID
((
string
)
$id
))
->
first
(
$columns
);
return
$this
->
where
(
'_id'
,
'='
,
$this
->
convertKey
(
$id
))
->
first
(
$columns
);
}
}
/**
/**
...
@@ -500,6 +500,22 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -500,6 +500,22 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
0
;
return
0
;
}
}
/**
* Convert a key to MongoID if needed
*
* @param mixed $id
* @return mixed
*/
protected
function
convertKey
(
$id
)
{
if
(
is_string
(
$id
)
&&
strlen
(
$id
)
===
24
&&
ctype_xdigit
(
$id
))
{
return
new
MongoId
(
$id
);
}
return
$id
;
}
/**
/**
* Compile the where array
* Compile the where array
*
*
...
@@ -522,13 +538,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -522,13 +538,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
{
{
foreach
(
$where
[
'values'
]
as
&
$value
)
foreach
(
$where
[
'values'
]
as
&
$value
)
{
{
$value
=
(
$value
instanceof
MongoID
)
?
$value
:
new
MongoID
(
$value
);
$value
=
$this
->
convertKey
(
$value
);
}
}
}
}
// Single value
// Single value
else
else
{
{
$where
[
'value'
]
=
(
$where
[
'value'
]
instanceof
MongoID
)
?
$where
[
'value'
]
:
new
MongoID
(
$where
[
'value'
]);
$where
[
'value'
]
=
$this
->
convertKey
(
$where
[
'value'
]);
}
}
}
}
...
...
tests/QueryTest.php
View file @
03cee24c
...
@@ -228,4 +228,19 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -228,4 +228,19 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
array
(
'sharp'
,
'round'
),
$types
);
$this
->
assertEquals
(
array
(
'sharp'
,
'round'
),
$types
);
}
}
public
function
testCustomId
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'_id'
=>
'knife'
,
'type'
=>
'sharp'
,
'amount'
=>
34
),
array
(
'_id'
=>
'fork'
,
'type'
=>
'sharp'
,
'amount'
=>
20
),
array
(
'_id'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
3
)
));
$item
=
DB
::
collection
(
'items'
)
->
find
(
'knife'
);
$this
->
assertEquals
(
'knife'
,
$item
[
'_id'
]);
$item
=
DB
::
collection
(
'items'
)
->
where
(
'_id'
,
'fork'
)
->
first
();
$this
->
assertEquals
(
'fork'
,
$item
[
'_id'
]);
}
}
}
\ No newline at end of file
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