Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
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
Yeray Santana Hualde
syncEnrollments
Commits
cd71c1e5
Commit
cd71c1e5
authored
Mar 29, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added sync method to has many and belongs to entity relationship.
parent
c6125edc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
0 deletions
+45
-0
has_many_and_belongs_to.php
...tabase/eloquent/relationships/has_many_and_belongs_to.php
+45
-0
No files found.
laravel/database/eloquent/relationships/has_many_and_belongs_to.php
View file @
cd71c1e5
...
...
@@ -86,6 +86,51 @@ class Has_Many_And_Belongs_To extends Relationship {
return
$this
->
insert_joining
(
$joining
);
}
/**
* Detach a record from the joining table of the association.
*
* @param int $ids
* @return bool
*/
public
function
detach
(
$ids
)
{
if
(
!
is_array
(
$ids
))
$ids
=
array
(
$ids
);
return
$this
->
pivot
()
->
where_in
(
$this
->
other_key
(),
$ids
)
->
delete
();
}
/**
* Sync the joining table with the array of given IDs.
*
* @param array $ids
* @return bool
*/
public
function
sync
(
$ids
)
{
$current
=
$this
->
pivot
()
->
lists
(
$this
->
other_key
());
// First we need to attach any of the associated models that are not currently
// in the joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we insert.
foreach
(
$ids
as
$id
)
{
if
(
!
in_array
(
$id
,
$current
))
{
$this
->
attach
(
$id
);
}
}
// Next we will take the difference of the current and given IDs and detach
// all of the entities that exists in the current array but are not in
// the array of IDs given to the method, finishing the sync.
$detach
=
array_diff
(
$current
,
$ids
);
if
(
count
(
$detach
)
>
0
)
{
$this
->
detach
(
array_diff
(
$current
,
$ids
));
}
}
/**
* Insert a new record for the association.
*
...
...
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