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
9cf8823f
Commit
9cf8823f
authored
Nov 08, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Switching to Carbon like Laravel
parent
784758eb
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
46 deletions
+71
-46
Model.php
src/Jenssegers/Mongodb/Model.php
+59
-36
ConnectionTest.php
tests/ConnectionTest.php
+2
-2
ModelTest.php
tests/ModelTest.php
+10
-8
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
9cf8823f
...
@@ -8,6 +8,7 @@ use Jenssegers\Mongodb\DatabaseManager as Resolver;
...
@@ -8,6 +8,7 @@ use Jenssegers\Mongodb\DatabaseManager as Resolver;
use
Jenssegers\Mongodb\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Carbon\Carbon
;
use
DateTime
;
use
DateTime
;
use
MongoId
;
use
MongoId
;
use
MongoDate
;
use
MongoDate
;
...
@@ -66,19 +67,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -66,19 +67,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
*/
protected
function
asDateTime
(
$value
)
protected
function
asDateTime
(
$value
)
{
{
// Convert
MongoDate to
timestamp
// Convert timestamp
if
(
$value
instanceof
MongoDate
)
if
(
is_numeric
(
$value
)
)
{
{
$value
=
$value
->
sec
;
return
Carbon
::
createFromTimestamp
(
$value
)
;
}
}
// Convert timestamp to string for DateTime
// Convert string
if
(
is_int
(
$value
))
if
(
is_string
(
$value
))
{
return
new
Carbon
(
$value
);
}
// Convert MongoDate
if
(
$value
instanceof
MongoDate
)
{
{
$value
=
"@
$value
"
;
return
Carbon
::
createFromTimestamp
(
$value
->
sec
)
;
}
}
return
new
DateTim
e
(
$value
);
return
Carbon
::
instanc
e
(
$value
);
}
}
/**
/**
...
@@ -118,15 +125,18 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -118,15 +125,18 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*
*
* @param string $related
* @param string $related
* @param string $foreignKey
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
*/
public
function
hasOne
(
$related
,
$foreignKey
=
null
)
public
function
hasOne
(
$related
,
$foreignKey
=
null
,
$localKey
=
null
)
{
{
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$instance
=
new
$related
;
$instance
=
new
$related
;
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
}
/**
/**
...
@@ -134,15 +144,18 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -134,15 +144,18 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*
*
* @param string $related
* @param string $related
* @param string $foreignKey
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
*/
public
function
hasMany
(
$related
,
$foreignKey
=
null
)
public
function
hasMany
(
$related
,
$foreignKey
=
null
,
$localKey
=
null
)
{
{
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$instance
=
new
$related
;
$instance
=
new
$related
;
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
}
/**
/**
...
@@ -150,30 +163,40 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -150,30 +163,40 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*
*
* @param string $related
* @param string $related
* @param string $foreignKey
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
*/
public
function
belongsTo
(
$related
,
$foreignKey
=
null
)
public
function
belongsTo
(
$related
,
$foreignKey
=
null
,
$otherKey
=
null
,
$relation
=
null
)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if
(
is_null
(
$relation
))
{
{
list
(,
$caller
)
=
debug_backtrace
(
false
);
list
(,
$caller
)
=
debug_backtrace
(
false
);
$relation
=
$caller
[
'function'
];
}
// If no foreign key was supplied, we can use a backtrace to guess the proper
// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
// when combined with an "_id" should conventionally match the columns.
$relation
=
$caller
[
'function'
];
if
(
is_null
(
$foreignKey
))
if
(
is_null
(
$foreignKey
))
{
{
$foreignKey
=
snake_case
(
$relation
)
.
'_id'
;
$foreignKey
=
snake_case
(
$relation
)
.
'_id'
;
}
}
$instance
=
new
$related
;
// Once we have the foreign key names, we'll just create a new Eloquent query
// Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will
// for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations.
// actually be responsible for retrieving and hydrating every relations.
$instance
=
new
$related
;
$query
=
$instance
->
newQuery
();
$query
=
$instance
->
newQuery
();
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$relation
);
$otherKey
=
$otherKey
?:
$instance
->
getKeyName
();
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$otherKey
,
$relation
);
}
}
/**
/**
...
...
tests/ConnectionTest.php
View file @
9cf8823f
...
@@ -46,7 +46,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
...
@@ -46,7 +46,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$this
->
assertTrue
(
is_array
(
$dbs
));
$this
->
assertTrue
(
is_array
(
$dbs
));
}
}
public
function
testMultipleConnections
()
/*
public function testMultipleConnections()
{
{
global $app;
global $app;
...
@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
...
@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$hosts = $mongoclient->getHosts();
$hosts = $mongoclient->getHosts();
$this->assertEquals(1, count($hosts));
$this->assertEquals(1, count($hosts));
}
}
*/
public
function
testQueryLog
()
public
function
testQueryLog
()
{
{
...
...
tests/ModelTest.php
View file @
9cf8823f
<?php
<?php
use
Carbon
;
class
ModelTest
extends
PHPUnit_Framework_TestCase
{
class
ModelTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{}
public
function
setUp
()
{}
...
@@ -37,7 +39,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
...
@@ -37,7 +39,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this
->
assertTrue
(
isset
(
$user
->
_id
));
$this
->
assertTrue
(
isset
(
$user
->
_id
));
$this
->
assertNotEquals
(
''
,
(
string
)
$user
->
_id
);
$this
->
assertNotEquals
(
''
,
(
string
)
$user
->
_id
);
$this
->
assertNotEquals
(
0
,
strlen
((
string
)
$user
->
_id
));
$this
->
assertNotEquals
(
0
,
strlen
((
string
)
$user
->
_id
));
$this
->
assertInstanceOf
(
'
DateTime
'
,
$user
->
created_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$user
->
created_at
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
35
,
$user
->
age
);
$this
->
assertEquals
(
35
,
$user
->
age
);
...
@@ -57,8 +59,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
...
@@ -57,8 +59,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$check
->
save
();
$check
->
save
();
$this
->
assertEquals
(
true
,
$check
->
exists
);
$this
->
assertEquals
(
true
,
$check
->
exists
);
$this
->
assertInstanceOf
(
'
DateTime
'
,
$check
->
created_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$check
->
created_at
);
$this
->
assertInstanceOf
(
'
DateTime
'
,
$check
->
updated_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$check
->
updated_at
);
$this
->
assertEquals
(
1
,
User
::
count
());
$this
->
assertEquals
(
1
,
User
::
count
());
$this
->
assertEquals
(
'John Doe'
,
$check
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$check
->
name
);
...
@@ -229,7 +231,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
...
@@ -229,7 +231,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
1
,
$all
->
count
());
$this
->
assertEquals
(
1
,
$all
->
count
());
$check
=
$all
[
0
];
$check
=
$all
[
0
];
$this
->
assertInstanceOf
(
'
DateTime
'
,
$check
->
deleted_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$check
->
deleted_at
);
$this
->
assertEquals
(
true
,
$check
->
trashed
());
$this
->
assertEquals
(
true
,
$check
->
trashed
());
$check
->
restore
();
$check
->
restore
();
...
@@ -312,11 +314,11 @@ class ModelTest extends PHPUnit_Framework_TestCase {
...
@@ -312,11 +314,11 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public
function
testDates
()
public
function
testDates
()
{
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'birthday'
=>
new
DateTime
(
'1980/1/1'
)));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'birthday'
=>
new
DateTime
(
'1980/1/1'
)));
$this
->
assertInstanceOf
(
'
DateTime
'
,
$user
->
birthday
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$user
->
birthday
);
// Re-fetch to be sure
$check
=
User
::
find
(
$user
->
_id
);
$
user
=
User
::
find
(
$user
->
_id
);
$
this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$check
->
birthday
);
$this
->
assert
InstanceOf
(
'DateTime'
,
$user
->
birthday
);
$this
->
assert
Equals
(
$user
->
birthday
,
$check
->
birthday
);
$user
=
User
::
where
(
'birthday'
,
'>'
,
new
DateTime
(
'1975/1/1'
))
->
first
();
$user
=
User
::
where
(
'birthday'
,
'>'
,
new
DateTime
(
'1975/1/1'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
...
...
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