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
Hide 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;
use
Jenssegers\Mongodb\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Carbon\Carbon
;
use
DateTime
;
use
MongoId
;
use
MongoDate
;
...
...
@@ -66,19 +67,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
protected
function
asDateTime
(
$value
)
{
// Convert MongoDate to timestamp
if
(
$value
instanceof
MongoDate
)
// Convert timestamp
if
(
is_numeric
(
$value
))
{
return
Carbon
::
createFromTimestamp
(
$value
);
}
// Convert string
if
(
is_string
(
$value
))
{
$value
=
$value
->
sec
;
return
new
Carbon
(
$value
)
;
}
// Convert
timestamp to string for DateTim
e
if
(
is_int
(
$value
)
)
// Convert
MongoDat
e
if
(
$value
instanceof
MongoDate
)
{
$value
=
"@
$value
"
;
return
Carbon
::
createFromTimestamp
(
$value
->
sec
)
;
}
return
new
DateTim
e
(
$value
);
return
Carbon
::
instanc
e
(
$value
);
}
/**
...
...
@@ -114,66 +121,82 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
}
/**
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public
function
hasOne
(
$related
,
$foreignKey
=
null
)
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public
function
hasOne
(
$related
,
$foreignKey
=
null
,
$localKey
=
null
)
{
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$instance
=
new
$related
;
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
/**
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public
function
hasMany
(
$related
,
$foreignKey
=
null
)
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public
function
hasMany
(
$related
,
$foreignKey
=
null
,
$localKey
=
null
)
{
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$instance
=
new
$related
;
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public
function
belongsTo
(
$related
,
$foreignKey
=
null
)
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public
function
belongsTo
(
$related
,
$foreignKey
=
null
,
$otherKey
=
null
,
$relation
=
null
)
{
list
(,
$caller
)
=
debug_backtrace
(
false
);
// 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
);
$relation
=
$caller
[
'function'
];
}
// 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
// when combined with an "_id" should conventionally match the columns.
$relation
=
$caller
[
'function'
];
if
(
is_null
(
$foreignKey
))
{
$foreignKey
=
snake_case
(
$relation
)
.
'_id'
;
}
$instance
=
new
$related
;
// 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
// actually be responsible for retrieving and hydrating every relations.
$instance
=
new
$related
;
$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 {
$this
->
assertTrue
(
is_array
(
$dbs
));
}
public
function
testMultipleConnections
()
/*
public function testMultipleConnections()
{
global $app;
...
...
@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$hosts = $mongoclient->getHosts();
$this->assertEquals(1, count($hosts));
}
}
*/
public
function
testQueryLog
()
{
...
...
tests/ModelTest.php
View file @
9cf8823f
<?php
use
Carbon
;
class
ModelTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{}
...
...
@@ -37,7 +39,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this
->
assertTrue
(
isset
(
$user
->
_id
));
$this
->
assertNotEquals
(
''
,
(
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
(
35
,
$user
->
age
);
...
...
@@ -57,8 +59,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$check
->
save
();
$this
->
assertEquals
(
true
,
$check
->
exists
);
$this
->
assertInstanceOf
(
'
DateTime
'
,
$check
->
created_at
);
$this
->
assertInstanceOf
(
'
DateTime
'
,
$check
->
updated_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$check
->
created_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$check
->
updated_at
);
$this
->
assertEquals
(
1
,
User
::
count
());
$this
->
assertEquals
(
'John Doe'
,
$check
->
name
);
...
...
@@ -229,7 +231,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
1
,
$all
->
count
());
$check
=
$all
[
0
];
$this
->
assertInstanceOf
(
'
DateTime
'
,
$check
->
deleted_at
);
$this
->
assertInstanceOf
(
'
Carbon\Carbon
'
,
$check
->
deleted_at
);
$this
->
assertEquals
(
true
,
$check
->
trashed
());
$check
->
restore
();
...
...
@@ -312,11 +314,11 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public
function
testDates
()
{
$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
$
user
=
User
::
find
(
$user
->
_id
);
$this
->
assert
InstanceOf
(
'DateTime'
,
$user
->
birthday
);
$check
=
User
::
find
(
$user
->
_id
);
$
this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$check
->
birthday
);
$this
->
assert
Equals
(
$user
->
birthday
,
$check
->
birthday
);
$user
=
User
::
where
(
'birthday'
,
'>'
,
new
DateTime
(
'1975/1/1'
))
->
first
();
$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