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
e89d2263
Commit
e89d2263
authored
Nov 20, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Slimming down the BelongsToMany class
parent
12e7cb2a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
187 additions
and
335 deletions
+187
-335
BelongsToMany.php
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+20
-168
RelationsTest.php
tests/RelationsTest.php
+167
-167
No files found.
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
View file @
e89d2263
<?php
namespace
Jenssegers\Mongodb\Relations
;
<?php
namespace
Jenssegers\Mongodb\Relations
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
...
@@ -7,29 +8,14 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMan
...
@@ -7,29 +8,14 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMan
class
BelongsToMany
extends
EloquentBelongsToMany
{
class
BelongsToMany
extends
EloquentBelongsToMany
{
/**
/**
*
Execute the query as a "select" statement
.
*
Hydrate the pivot table relationship on the models
.
*
*
* @param array $
column
s
* @param array $
model
s
* @return
\Illuminate\Database\Eloquent\Collection
* @return
void
*/
*/
public
function
get
(
$columns
=
array
(
'*'
))
protected
function
hydratePivotRelation
(
array
$models
)
{
// First we'll add the proper select columns onto the query so it is run with
// the proper columns. Then, we will get the results and hydrate out pivot
// models with the result of those columns as a separate model relation.
$select
=
$this
->
getSelectColumns
(
$columns
);
$models
=
$this
->
query
->
addSelect
(
$select
)
->
getModels
();
// If we actually found models we will also eager load any relationships that
// have been specified as needing to be eager loaded. This will solve the
// n + 1 query problem for the developer and also increase performance.
if
(
count
(
$models
)
>
0
)
{
{
$models
=
$this
->
query
->
eagerLoadRelations
(
$models
);
// Do nothing
}
return
$this
->
related
->
newCollection
(
$models
);
}
}
/**
/**
...
@@ -42,26 +28,6 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -42,26 +28,6 @@ class BelongsToMany extends EloquentBelongsToMany {
return
$columns
;
return
$columns
;
}
}
/**
* Get a paginator for the "select" statement.
*
* @param int $perPage
* @param array $columns
* @return \Illuminate\Pagination\Paginator
*/
public
function
paginate
(
$perPage
=
null
,
$columns
=
array
(
'*'
))
{
$this
->
query
->
addSelect
(
$this
->
getSelectColumns
(
$columns
));
// When paginating results, we need to add the pivot columns to the query and
// then hydrate into the pivot objects once the results have been gathered
// from the database since this isn't performed by the Eloquent builder.
$pager
=
$this
->
query
->
paginate
(
$perPage
,
$columns
);
return
$pager
;
}
/**
/**
* Set the base constraints on the relation query.
* Set the base constraints on the relation query.
*
*
...
@@ -77,55 +43,6 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -77,55 +43,6 @@ class BelongsToMany extends EloquentBelongsToMany {
}
}
}
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public
function
addEagerConstraints
(
array
$models
)
{
$this
->
query
->
whereIn
(
$this
->
getForeignKey
(),
$this
->
getKeys
(
$models
));
}
/**
* Save a new model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public
function
save
(
Model
$model
,
array
$joining
=
array
(),
$touch
=
true
)
{
$model
->
save
(
array
(
'touch'
=>
false
));
$this
->
attach
(
$model
->
getKey
(),
$joining
,
$touch
);
return
$model
;
}
/**
* Create a new instance of the related model.
*
* @param array $attributes
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public
function
create
(
array
$attributes
,
array
$joining
=
array
(),
$touch
=
true
)
{
$instance
=
$this
->
related
->
newInstance
(
$attributes
);
// Save the new instance before we attach it to other models
$instance
->
save
(
array
(
'touch'
=>
false
));
// Attach to the parent instance
$this
->
attach
(
$instance
->
_id
,
$attributes
,
$touch
);
return
$instance
;
}
/**
/**
* Sync the intermediate tables with a list of IDs.
* Sync the intermediate tables with a list of IDs.
*
*
...
@@ -164,29 +81,6 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -164,29 +81,6 @@ class BelongsToMany extends EloquentBelongsToMany {
$this
->
touchIfTouching
();
$this
->
touchIfTouching
();
}
}
/**
* Format the sync list so that it is keyed by ID.
*
* @param array $records
* @return array
*/
protected
function
formatSyncList
(
array
$records
)
{
$results
=
array
();
foreach
(
$records
as
$id
=>
$attributes
)
{
if
(
!
is_array
(
$attributes
))
{
list
(
$id
,
$attributes
)
=
array
(
$attributes
,
array
());
}
$results
[
$id
]
=
$attributes
;
}
return
$results
;
}
/**
/**
* Attach all of the IDs that aren't in the current array.
* Attach all of the IDs that aren't in the current array.
*
*
...
@@ -300,38 +194,6 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -300,38 +194,6 @@ class BelongsToMany extends EloquentBelongsToMany {
return
count
(
$ids
);
return
count
(
$ids
);
}
}
/**
* If we're touching the parent model, touch.
*
* @return void
*/
public
function
touchIfTouching
()
{
if
(
$this
->
touchingParent
())
$this
->
getParent
()
->
touch
();
if
(
$this
->
getParent
()
->
touches
(
$this
->
relationName
))
$this
->
touch
();
}
/**
* Determine if we should touch the parent on sync.
*
* @return bool
*/
protected
function
touchingParent
()
{
return
$this
->
getRelated
()
->
touches
(
$this
->
guessInverseRelation
());
}
/**
* Attempt to guess the name of the inverse of the relation.
*
* @return string
*/
protected
function
guessInverseRelation
()
{
return
strtolower
(
str_plural
(
class_basename
(
$this
->
getParent
())));
}
/**
/**
* Create a new query builder for the parent
* Create a new query builder for the parent
*
*
...
@@ -370,16 +232,6 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -370,16 +232,6 @@ class BelongsToMany extends EloquentBelongsToMany {
return
$dictionary
;
return
$dictionary
;
}
}
/**
* Get the related model's updated at column name.
*
* @return string
*/
public
function
getRelatedFreshUpdate
()
{
return
array
(
$this
->
related
->
getUpdatedAtColumn
()
=>
$this
->
related
->
freshTimestamp
());
}
/**
/**
* Get the fully qualified foreign key for the relation.
* Get the fully qualified foreign key for the relation.
*
*
...
...
tests/RelationsTest.php
View file @
e89d2263
...
@@ -31,7 +31,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -31,7 +31,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$items
=
$user
->
items
;
$items
=
$user
->
items
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertEquals
(
3
,
count
(
$items
));
}
}
public
function
testBelongsTo
()
public
function
testBelongsTo
()
{
{
...
...
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