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
80b01111
Commit
80b01111
authored
Oct 27, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding fluent schema builder support
parent
4c8d9084
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
152 additions
and
28 deletions
+152
-28
phpunit.xml
phpunit.xml
+11
-2
Blueprint.php
src/Jenssegers/Mongodb/Schema/Blueprint.php
+114
-19
Builder.php
src/Jenssegers/Mongodb/Schema/Builder.php
+10
-6
ConnectionTest.php
tests/ConnectionTest.php
+1
-1
SchemaTest.php
tests/SchemaTest.php
+16
-0
No files found.
phpunit.xml
View file @
80b01111
...
@@ -7,12 +7,21 @@
...
@@ -7,12 +7,21 @@
convertNoticesToExceptions=
"true"
convertNoticesToExceptions=
"true"
convertWarningsToExceptions=
"true"
convertWarningsToExceptions=
"true"
processIsolation=
"false"
processIsolation=
"false"
stopOnFailure=
"
fals
e"
stopOnFailure=
"
tru
e"
syntaxCheck=
"false"
syntaxCheck=
"false"
>
>
<testsuites>
<testsuites>
<testsuite
name=
"
Laravel MongoDB Test Suite
"
>
<testsuite
name=
"
all
"
>
<directory>
tests/
</directory>
<directory>
tests/
</directory>
</testsuite>
</testsuite>
<testsuite
name=
"schema"
>
<directory>
tests/SchemaTest.php
</directory>
</testsuite>
<testsuite
name=
"seeder"
>
<directory>
tests/SeederTest.php
</directory>
</testsuite>
<testsuite
name=
"cache"
>
<directory>
tests/CacheTest.php
</directory>
</testsuite>
</testsuites>
</testsuites>
</phpunit>
</phpunit>
src/Jenssegers/Mongodb/Schema/Blueprint.php
View file @
80b01111
...
@@ -3,7 +3,14 @@
...
@@ -3,7 +3,14 @@
use
Closure
;
use
Closure
;
use
Illuminate\Database\Connection
;
use
Illuminate\Database\Connection
;
class
Blueprint
{
class
Blueprint
extends
\Illuminate\Database\Schema\Blueprint
{
/**
* The MongoConnection object for this blueprint.
*
* @var MongoConnection
*/
protected
$connection
;
/**
/**
* The MongoCollection object for this blueprint.
* The MongoCollection object for this blueprint.
...
@@ -12,6 +19,13 @@ class Blueprint {
...
@@ -12,6 +19,13 @@ class Blueprint {
*/
*/
protected
$collection
;
protected
$collection
;
/**
* Fluent columns
*
* @var array
*/
protected
$columns
=
array
();
/**
/**
* Create a new schema blueprint.
* Create a new schema blueprint.
*
*
...
@@ -21,6 +35,7 @@ class Blueprint {
...
@@ -21,6 +35,7 @@ class Blueprint {
*/
*/
public
function
__construct
(
Connection
$connection
,
$collection
)
public
function
__construct
(
Connection
$connection
,
$collection
)
{
{
$this
->
connection
=
$connection
;
$this
->
collection
=
$connection
->
getCollection
(
$collection
);
$this
->
collection
=
$connection
->
getCollection
(
$collection
);
}
}
...
@@ -31,11 +46,26 @@ class Blueprint {
...
@@ -31,11 +46,26 @@ class Blueprint {
* @param array $options
* @param array $options
* @return bool
* @return bool
*/
*/
public
function
index
(
$columns
,
$options
=
array
())
public
function
index
(
$columns
=
null
,
$options
=
array
())
{
{
$result
=
$this
->
collection
->
ensureIndex
(
$columns
,
$options
);
$columns
=
$this
->
fluent
(
$columns
);
// Columns are passed as a default array
if
(
is_array
(
$columns
)
&&
is_int
(
key
(
$columns
)))
{
// Transform the columns to the required array format
$transform
=
array
();
foreach
(
$columns
as
$column
)
{
$transform
[
$column
]
=
1
;
}
$columns
=
$transform
;
}
return
(
1
==
(
int
)
$result
[
'ok'
]);
$this
->
collection
->
ensureIndex
(
$columns
,
$options
);
return
$this
;
}
}
/**
/**
...
@@ -44,11 +74,16 @@ class Blueprint {
...
@@ -44,11 +74,16 @@ class Blueprint {
* @param string|array $columns
* @param string|array $columns
* @return bool
* @return bool
*/
*/
public
function
dropIndex
(
$columns
)
public
function
dropIndex
(
$columns
=
null
)
{
{
$result
=
$this
->
collection
->
deleteIndex
(
$columns
);
$columns
=
$this
->
fluent
(
$columns
);
foreach
(
$columns
as
$column
)
{
$this
->
collection
->
deleteIndex
(
$column
);
}
return
(
1
==
(
int
)
$result
[
'ok'
])
;
return
$this
;
}
}
/**
/**
...
@@ -57,44 +92,70 @@ class Blueprint {
...
@@ -57,44 +92,70 @@ class Blueprint {
* @param string|array $columns
* @param string|array $columns
* @return bool
* @return bool
*/
*/
public
function
unique
(
$columns
)
public
function
unique
(
$columns
=
null
)
{
{
return
$this
->
index
(
$columns
,
array
(
'unique'
=>
true
));
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'unique'
=>
true
));
return
$this
;
}
}
/**
/**
* Specify a non blocking index for the collection.
* Specify a non blocking index for the collection.
*
*
* @param string|array $columns
* @param string|array $columns
* @return bool
* @return bool
*/
*/
public
function
background
(
$columns
)
public
function
background
(
$columns
=
null
)
{
{
return
$this
->
index
(
$columns
,
array
(
'background'
=>
true
));
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'background'
=>
true
));
return
$this
;
}
}
/**
/**
* Specify a sparse index for the collection.
* Specify a sparse index for the collection.
*
*
* @param string|array $columns
* @param string|array $columns
* @return bool
* @return bool
*/
*/
public
function
sparse
(
$columns
)
public
function
sparse
(
$columns
=
null
)
{
{
return
$this
->
index
(
$columns
,
array
(
'sparse'
=>
true
));
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'sparse'
=>
true
));
return
$this
;
}
}
/**
/**
* Specify the number of seconds after wich a document should be considered expired based,
* Specify the number of seconds after wich a document should be considered expired based,
* on the given single-field index containing a date.
* on the given single-field index containing a date.
*
*
* @param string|array $columns
* @param string|array $columns
* @param int $seconds
* @param int $seconds
* @return bool
* @return bool
*/
*/
public
function
expire
(
$columns
,
$seconds
)
public
function
expire
(
$columns
,
$seconds
)
{
{
return
$this
->
index
(
$columns
,
array
(
'expireAfterSeconds'
=>
$seconds
));
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'expireAfterSeconds'
=>
$seconds
));
return
$this
;
}
/**
* Indicate that the table needs to be created.
*
* @return bool
*/
public
function
create
()
{
$collection
=
$this
->
collection
->
getName
();
// Ensure the collection is created
$db
=
$this
->
connection
->
getMongoDB
();
$db
->
createCollection
(
$collection
);
}
}
/**
/**
...
@@ -104,9 +165,43 @@ class Blueprint {
...
@@ -104,9 +165,43 @@ class Blueprint {
*/
*/
public
function
drop
()
public
function
drop
()
{
{
$result
=
$this
->
collection
->
drop
();
$this
->
collection
->
drop
();
}
return
(
1
==
(
int
)
$result
[
'ok'
]);
/**
* Add a new column to the blueprint.
*
* @param string $type
* @param string $name
* @param array $parameters
* @return Blueprint
*/
protected
function
addColumn
(
$type
,
$name
,
array
$parameters
=
array
())
{
$this
->
fluent
(
$name
);
return
$this
;
}
/**
* Allow fluent columns
*
* @param string|array $columns
* @return string|array
*/
protected
function
fluent
(
$columns
=
null
)
{
if
(
is_null
(
$columns
))
{
return
$this
->
columns
;
}
else
if
(
is_string
(
$columns
))
{
return
$this
->
columns
=
array
(
$columns
);
}
else
{
return
$this
->
columns
=
$columns
;
}
}
}
}
}
src/Jenssegers/Mongodb/Schema/Builder.php
View file @
80b01111
...
@@ -51,7 +51,10 @@ class Builder extends \Illuminate\Database\Schema\Builder {
...
@@ -51,7 +51,10 @@ class Builder extends \Illuminate\Database\Schema\Builder {
{
{
$blueprint
=
$this
->
createBlueprint
(
$collection
);
$blueprint
=
$this
->
createBlueprint
(
$collection
);
return
$callback
(
$blueprint
);
if
(
$callback
)
{
$callback
(
$blueprint
);
}
}
}
/**
/**
...
@@ -75,12 +78,13 @@ class Builder extends \Illuminate\Database\Schema\Builder {
...
@@ -75,12 +78,13 @@ class Builder extends \Illuminate\Database\Schema\Builder {
*/
*/
public
function
create
(
$collection
,
Closure
$callback
=
null
)
public
function
create
(
$collection
,
Closure
$callback
=
null
)
{
{
$db
=
$this
->
connection
->
getMongoDB
();
$blueprint
=
$this
->
createBlueprint
(
$collection
);
$db
->
createCollection
(
$collection
);
$blueprint
->
create
();
if
(
$callback
)
if
(
$callback
)
{
{
return
$this
->
collection
(
$collection
,
$callback
);
$callback
(
$blueprint
);
}
}
}
}
...
@@ -93,7 +97,7 @@ class Builder extends \Illuminate\Database\Schema\Builder {
...
@@ -93,7 +97,7 @@ class Builder extends \Illuminate\Database\Schema\Builder {
public
function
drop
(
$collection
)
public
function
drop
(
$collection
)
{
{
$blueprint
=
$this
->
createBlueprint
(
$collection
);
$blueprint
=
$this
->
createBlueprint
(
$collection
);
return
$blueprint
->
drop
();
return
$blueprint
->
drop
();
}
}
...
@@ -108,4 +112,4 @@ class Builder extends \Illuminate\Database\Schema\Builder {
...
@@ -108,4 +112,4 @@ class Builder extends \Illuminate\Database\Schema\Builder {
return
new
Blueprint
(
$this
->
connection
,
$collection
);
return
new
Blueprint
(
$this
->
connection
,
$collection
);
}
}
}
}
\ No newline at end of file
tests/ConnectionTest.php
View file @
80b01111
...
@@ -52,7 +52,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
...
@@ -52,7 +52,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
# Add fake host
# Add fake host
$db
=
$app
[
'config'
][
'database.connections'
][
'mongodb'
];
$db
=
$app
[
'config'
][
'database.connections'
][
'mongodb'
];
$db
[
'host'
]
=
array
(
$db
[
'host'
],
'
1.2.3.4
'
);
$db
[
'host'
]
=
array
(
$db
[
'host'
],
'
0.0.0.0
'
);
$connection
=
new
Connection
(
$db
);
$connection
=
new
Connection
(
$db
);
$mongoclient
=
$connection
->
getMongoClient
();
$mongoclient
=
$connection
->
getMongoClient
();
...
...
tests/SchemaTest.php
View file @
80b01111
...
@@ -92,6 +92,22 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
...
@@ -92,6 +92,22 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
60
,
$index
[
'expireAfterSeconds'
]);
$this
->
assertEquals
(
60
,
$index
[
'expireAfterSeconds'
]);
}
}
public
function
testFluent
()
{
Schema
::
collection
(
'newcollection'
,
function
(
$collection
)
{
$collection
->
string
(
'email'
)
->
index
();
$collection
->
string
(
'token'
)
->
index
();
$collection
->
timestamp
(
'created_at'
);
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'email'
);
$this
->
assertEquals
(
1
,
$index
[
'key'
][
'email'
]);
$index
=
$this
->
getIndex
(
'newcollection'
,
'token'
);
$this
->
assertEquals
(
1
,
$index
[
'key'
][
'token'
]);
}
protected
function
getIndex
(
$collection
,
$name
)
protected
function
getIndex
(
$collection
,
$name
)
{
{
$collection
=
DB
::
getCollection
(
$collection
);
$collection
=
DB
::
getCollection
(
$collection
);
...
...
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