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
257b92fe
Commit
257b92fe
authored
Nov 04, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updating model to 4.1 methods, check issue 63
parent
6e24848b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
39 deletions
+55
-39
Model.php
src/Jenssegers/Mongodb/Model.php
+45
-29
BelongsTo.php
src/Jenssegers/Mongodb/Relations/BelongsTo.php
+8
-8
ConnectionTest.php
tests/ConnectionTest.php
+2
-2
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
257b92fe
...
...
@@ -114,66 +114,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
);
}
/**
...
...
src/Jenssegers/Mongodb/Relations/BelongsTo.php
View file @
257b92fe
...
...
@@ -3,10 +3,10 @@
class
BelongsTo
extends
\Illuminate\Database\Eloquent\Relations\BelongsTo
{
/**
* Set the base constraints on the relation query.
*
* @return void
*/
* Set the base constraints on the relation query.
*
* @return void
*/
public
function
addConstraints
()
{
if
(
static
::
$constraints
)
...
...
@@ -14,9 +14,9 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$
key
=
$this
->
related
->
getKeyNam
e
();
$
table
=
$this
->
related
->
getTabl
e
();
$this
->
query
->
where
(
$
k
ey
,
'='
,
$this
->
parent
->
{
$this
->
foreignKey
});
$this
->
query
->
where
(
$
this
->
otherK
ey
,
'='
,
$this
->
parent
->
{
$this
->
foreignKey
});
}
}
...
...
@@ -31,9 +31,9 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
// We'll grab the primary key name of the related models since it could be set to
// a non-standard name and not "id". We will then construct the constraint for
// our eagerly loading query so it returns the proper models from execution.
$key
=
$this
->
related
->
getKeyName
()
;
$key
=
$this
->
otherKey
;
$this
->
query
->
whereIn
(
$key
,
$this
->
getEagerModelKeys
(
$models
));
}
}
\ No newline at end of file
}
tests/ConnectionTest.php
View file @
257b92fe
...
...
@@ -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
()
{
...
...
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