Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
Overview
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
453d4154
Commit
453d4154
authored
Mar 22, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added "pivot" method to has_many_and_belongs_to.
parent
1929cadc
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
8 deletions
+22
-8
laravel/database/eloquent/query.php
+1
-1
laravel/database/eloquent/relationships/has_many_and_belongs_to.php
+21
-7
No files found.
laravel/database/eloquent/query.php
View file @
453d4154
...
...
@@ -136,7 +136,7 @@ class Query {
// any pivot columns that are on the model.
if
(
$this
instanceof
Relationships\Has_Many_And_Belongs_To
)
{
$this
->
pivot
(
$models
);
$this
->
hydrate_
pivot
(
$models
);
}
return
$models
;
...
...
laravel/database/eloquent/relationships/has_many_and_belongs_to.php
View file @
453d4154
...
...
@@ -108,9 +108,11 @@ class Has_Many_And_Belongs_To extends Relationship {
// If the insert was successful, we'll insert a record into the joining table
// using the new ID that was just inserted into the related table, allowing
// the developer to not worry about maintaining the join table.
if
(
$model
instanceof
Model
and
is_numeric
(
$id
=
$model
->
get_key
())
)
if
(
$model
instanceof
Model
)
{
$result
=
$this
->
insert_joining
(
array_merge
(
$this
->
join_record
(
$id
),
$joining
));
$joining
=
array_merge
(
$this
->
join_record
(
$id
),
$joining
);
$result
=
$this
->
insert_joining
(
$joining
);
}
return
$model
instanceof
Model
and
$result
;
...
...
@@ -279,7 +281,7 @@ class Has_Many_And_Belongs_To extends Relationship {
* @param array $results
* @return void
*/
protected
function
pivot
(
&
$results
)
protected
function
hydrate_
pivot
(
&
$results
)
{
foreach
(
$results
as
&
$result
)
{
...
...
@@ -288,18 +290,20 @@ class Has_Many_And_Belongs_To extends Relationship {
// the pivot table that may need to be accessed by the developer.
$pivot
=
new
Pivot
(
$this
->
joining
);
$attributes
=
array_filter
(
$result
->
attributes
,
function
(
$attribute
)
{
return
starts_with
(
$attribute
,
'pivot_'
);
});
// If the attribute key starts with "pivot_", we know this is a column on
// the pivot table, so we will move it to the Pivot model and purge it
// from the model since it actually belongs to the pivot.
foreach
(
$result
->
attributes
as
$key
=>
$value
)
{
if
(
starts_with
(
$key
,
'pivot_'
))
foreach
(
$attributes
as
$key
=>
$value
)
{
$pivot
->
{
substr
(
$key
,
6
)}
=
$value
;
$result
->
purge
(
$key
);
}
}
// Once we have completed hydrating the pivot model instance, we'll set
// it on the result model's relationships array so the developer can
...
...
@@ -331,6 +335,16 @@ class Has_Many_And_Belongs_To extends Relationship {
}
/**
* Get a model instance of the pivot table for the relationship.
*
* @return Pivot
*/
public
function
pivot
()
{
return
new
Pivot
(
$this
->
joining
);
}
/**
* Get the other or associated key for the relationship.
*
* @return string
...
...
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