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
bbb79909
Commit
bbb79909
authored
Nov 20, 2013
by
unknown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Model updated
parent
3046a095
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
62 deletions
+76
-62
Model.php
src/Jenssegers/Mongodb/Model.php
+76
-62
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
bbb79909
...
@@ -7,8 +7,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
...
@@ -7,8 +7,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use
Jenssegers\Mongodb\DatabaseManager
as
Resolver
;
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
Jenssegers\Mongodb\Relations\BelongsToMany
;
use
Carbon\Carbon
;
use
DateTime
;
use
DateTime
;
use
MongoId
;
use
MongoId
;
use
MongoDate
;
use
MongoDate
;
...
@@ -67,25 +67,19 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -67,25 +67,19 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
*/
protected
function
asDateTime
(
$value
)
protected
function
asDateTime
(
$value
)
{
{
// Convert timestamp
// Convert MongoDate to timestamp
if
(
is_numeric
(
$value
))
if
(
$value
instanceof
MongoDate
)
{
return
Carbon
::
createFromTimestamp
(
$value
);
}
// Convert string
if
(
is_string
(
$value
))
{
{
return
new
Carbon
(
$value
)
;
$value
=
$value
->
sec
;
}
}
// Convert
MongoDat
e
// Convert
timestamp to string for DateTim
e
if
(
$value
instanceof
MongoDate
)
if
(
is_int
(
$value
)
)
{
{
return
Carbon
::
createFromTimestamp
(
$value
->
sec
)
;
$value
=
"@
$value
"
;
}
}
return
Carbon
::
instanc
e
(
$value
);
return
new
DateTim
e
(
$value
);
}
}
/**
/**
...
@@ -121,83 +115,104 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -121,83 +115,104 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
}
}
/**
/**
* Define a one-to-one relationship.
* Define a one-to-one relationship.
*
*
* @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
;
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
}
/**
/**
* Define a one-to-many relationship.
* Define a one-to-many relationship.
*
*
* @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
;
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
}
/**
/**
* Define an inverse one-to-one or many relationship.
* Define an inverse one-to-one or many relationship.
*
*
* @param string $related
* @param string $related
* @param string $foreignKey
* @param string $foreignKey
* @param string $otherKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @param string $relation
*/
* @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
list
(,
$caller
)
=
debug_backtrace
(
false
);
// 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
// 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.
$
query
=
$instance
->
newQuery
()
;
$
instance
=
new
$related
;
$
otherKey
=
$otherKey
?:
$instance
->
getKeyName
();
$
query
=
$instance
->
newQuery
();
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$
otherKey
,
$
relation
);
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$relation
);
}
}
/**
* Define a many-to-many relationship.
*
* @param string $related
* @param string $table
* @param string $foreignKey
* @param string $otherKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public
function
belongsToMany
(
$related
,
$collection
=
null
,
$foreignKey
=
null
,
$otherKey
=
null
)
{
$caller
=
$this
->
getBelongsToManyCaller
();
// First, we'll need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we'll make the query
// instances as well as the relationship instances we need for this.
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
()
.
's'
;
$instance
=
new
$related
;
$otherKey
=
$otherKey
?:
$instance
->
getForeignKey
()
.
's'
;
// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
if
(
is_null
(
$collection
))
{
$collection
=
snake_case
(
str_plural
(
class_basename
(
$related
)));
}
// Now we're ready to create a new query builder for the related model and
// the relationship instances for the relation. The relations will set
// appropriate query constraint and entirely manages the hydrations.
$query
=
$instance
->
newQuery
();
return
new
BelongsToMany
(
$query
,
$this
,
$collection
,
$foreignKey
,
$otherKey
,
$caller
[
'function'
]);
}
/**
/**
* Get a new query builder instance for the connection.
* Get a new query builder instance for the connection.
...
@@ -251,7 +266,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -251,7 +266,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
{
{
$this
->
__unset
(
$column
);
$this
->
__unset
(
$column
);
}
}
// Perform unset only on current document
// Perform unset only on current document
return
$query
=
$this
->
newQuery
()
->
where
(
$this
->
getKeyName
(),
$this
->
getKey
())
->
unset
(
$columns
);
return
$query
=
$this
->
newQuery
()
->
where
(
$this
->
getKeyName
(),
$this
->
getKey
())
->
unset
(
$columns
);
}
}
...
@@ -265,7 +280,6 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -265,7 +280,6 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
*/
public
function
__call
(
$method
,
$parameters
)
public
function
__call
(
$method
,
$parameters
)
{
{
// Unset method
if
(
$method
==
'unset'
)
if
(
$method
==
'unset'
)
{
{
return
call_user_func_array
(
array
(
$this
,
'dropColumn'
),
$parameters
);
return
call_user_func_array
(
array
(
$this
,
'dropColumn'
),
$parameters
);
...
@@ -274,4 +288,4 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
...
@@ -274,4 +288,4 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return
parent
::
__call
(
$method
,
$parameters
);
return
parent
::
__call
(
$method
,
$parameters
);
}
}
}
}
\ 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