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
12e7cb2a
Commit
12e7cb2a
authored
Nov 20, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #70 from DyeH/master
Many to Many relationships
parents
4d005877
0eed8964
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
674 additions
and
125 deletions
+674
-125
.gitignore
.gitignore
+2
-1
Model.php
src/Jenssegers/Mongodb/Model.php
+39
-1
BelongsToMany.php
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+402
-0
ModelTest.php
tests/ModelTest.php
+3
-0
RelationsTest.php
tests/RelationsTest.php
+202
-115
Client.php
tests/models/Client.php
+14
-0
User.php
tests/models/User.php
+12
-8
No files found.
.gitignore
View file @
12e7cb2a
...
...
@@ -5,3 +5,4 @@ composer.phar
composer.lock
*.sublime-project
*.sublime-workspace
*.project
src/Jenssegers/Mongodb/Model.php
View file @
12e7cb2a
...
...
@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use
Jenssegers\Mongodb\DatabaseManager
as
Resolver
;
use
Jenssegers\Mongodb\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Jenssegers\Mongodb\Relations\BelongsToMany
;
use
Carbon\Carbon
;
use
DateTime
;
...
...
@@ -199,6 +200,43 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$otherKey
,
$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.
*
...
...
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
0 → 100644
View file @
12e7cb2a
This diff is collapsed.
Click to expand it.
tests/ModelTest.php
View file @
12e7cb2a
...
...
@@ -312,12 +312,15 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public
function
testDates
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'birthday'
=>
new
DateTime
(
'1980/1/1'
)));
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$user
->
birthday
);
$check
=
User
::
find
(
$user
->
_id
);
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$check
->
birthday
);
$this
->
assertEquals
(
$user
->
birthday
,
$check
->
birthday
);
$user
=
User
::
where
(
'birthday'
,
'>'
,
new
DateTime
(
'1975/1/1'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
}
...
...
tests/RelationsTest.php
View file @
12e7cb2a
...
...
@@ -11,6 +11,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Book
::
truncate
();
Item
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
}
public
function
testHasMany
()
...
...
@@ -125,4 +126,90 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
public
function
testHasManyAndBelongsTo
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
->
clients
()
->
save
(
new
Client
(
array
(
'name'
=>
'Pork Pies Ltd.'
)));
$user
->
clients
()
->
create
(
array
(
'name'
=>
'Buffet Bar Inc.'
));
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
$client
=
Client
::
with
(
'users'
)
->
first
();
$clients
=
$client
->
getRelation
(
'users'
);
$users
=
$user
->
getRelation
(
'clients'
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$users
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$clients
);
$this
->
assertInstanceOf
(
'Client'
,
$users
[
0
]);
$this
->
assertInstanceOf
(
'User'
,
$clients
[
0
]);
$this
->
assertCount
(
2
,
$user
->
clients
);
$this
->
assertCount
(
1
,
$client
->
users
);
// Now create a new user to an existing client
$client
->
users
()
->
create
(
array
(
'name'
=>
'Jane Doe'
));
$otherClient
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
()
->
clients
()
->
get
();
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$otherClient
);
$this
->
assertInstanceOf
(
'Client'
,
$otherClient
[
0
]);
$this
->
assertCount
(
1
,
$otherClient
);
// Now attach an existing client to an existing user
$user
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
();
$client
=
Client
::
Where
(
'name'
,
'='
,
'Buffet Bar Inc.'
)
->
first
();
// Check the models are what they should be
$this
->
assertInstanceOf
(
'Client'
,
$client
);
$this
->
assertInstanceOf
(
'User'
,
$user
);
// Assert they are not attached
$this
->
assertFalse
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertFalse
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
// Attach the client to the user
$user
->
clients
()
->
attach
(
$client
);
// Get the new user model
$user
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
();
$client
=
Client
::
Where
(
'name'
,
'='
,
'Buffet Bar Inc.'
)
->
first
();
// Assert they are attached
$this
->
assertTrue
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertTrue
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
}
public
function
testHasManyAndBelongsToAttachesExistingModels
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'client_ids'
=>
array
(
'1234523'
)));
$clients
=
array
(
Client
::
create
(
array
(
'name'
=>
'Pork Pies Ltd.'
))
->
_id
,
Client
::
create
(
array
(
'name'
=>
'Buffet Bar Inc.'
))
->
_id
);
$moreClients
=
array
(
Client
::
create
(
array
(
'name'
=>
'Boloni Ltd.'
))
->
_id
,
Client
::
create
(
array
(
'name'
=>
'Meatballs Inc.'
))
->
_id
);
// Sync multiple records
$user
->
clients
()
->
sync
(
$clients
);
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
// Assert non attached ID's are detached succesfully
$this
->
assertFalse
(
in_array
(
'1234523'
,
$user
->
client_ids
));
// Assert there are two client objects in the relationship
$this
->
assertCount
(
2
,
$user
->
clients
);
$user
->
clients
()
->
sync
(
$moreClients
);
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
// Assert there are now 4 client objects in the relationship
$this
->
assertCount
(
4
,
$user
->
clients
);
}
}
tests/models/Client.php
0 → 100644
View file @
12e7cb2a
<?php
use
Jenssegers\Mongodb\Model
as
Eloquent
;
class
Client
extends
Eloquent
{
protected
$collection
=
'clients'
;
protected
static
$unguarded
=
true
;
public
function
users
()
{
return
$this
->
belongsToMany
(
'User'
);
}
}
\ No newline at end of file
tests/models/User.php
View file @
12e7cb2a
...
...
@@ -28,6 +28,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return
$this
->
hasOne
(
'Role'
);
}
public
function
clients
()
{
return
$this
->
belongsToMany
(
'Client'
);
}
/**
* Get the unique identifier for the user.
*
...
...
@@ -57,5 +62,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return
$this
->
email
;
}
}
\ 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