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
df709876
Commit
df709876
authored
Apr 03, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added sync method to has_many relationship.
parent
3518f686
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
0 deletions
+34
-0
has_many.php
laravel/database/eloquent/relationships/has_many.php
+34
-0
No files found.
laravel/database/eloquent/relationships/has_many.php
View file @
df709876
...
...
@@ -12,6 +12,40 @@ class Has_Many extends Has_One_Or_Many {
return
parent
::
get
();
}
/**
* Sync the association table with an array of models.
*
* @param array $models
* @return bool
*/
public
function
sync
(
array
$models
)
{
$current
=
$this
->
table
->
lists
(
$this
->
model
->
key
());
// When syncing the has many relationship, we'll just spin the attributes and
// create a fresh model instances for each row. If the "id" is set on the
// array of attributes, we'll set the exists flag to true for update.
foreach
(
$models
as
$attributes
)
{
$attributes
[
$this
->
foreign_key
()]
=
$this
->
base
->
get_key
();
$model
=
$this
->
fresh_model
(
$attributes
);
$id
=
$model
->
get_key
();
$model
->
exists
=
(
!
is_null
(
$id
)
and
in_array
(
$id
,
$current
));
// Before saving we'll force the entire model to be "dirty" so all of
// the attributes are saved. It shouldn't affect the updates as
// saving all the attributes shouldn't hurt anything.
$model
->
original
=
array
();
$model
->
save
();
}
return
true
;
}
/**
* Initialize a relationship on an array of parent models.
*
...
...
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