Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Added ability to define custom class to use for pivot models. #14293

Merged
merged 3 commits into from
Nov 9, 2016
Merged

[5.4] Added ability to define custom class to use for pivot models. #14293

merged 3 commits into from
Nov 9, 2016

Conversation

pstephan1187
Copy link
Contributor

Moved PR over from 5.2 branch: #13304

@pstephan1187 pstephan1187 changed the title Added ability to define custom class to use for pivot models. [5.3] Added ability to define custom class to use for pivot models. Jul 12, 2016
@taylorotwell
Copy link
Member

Do we have any way to get tests for this?

@pstephan1187
Copy link
Contributor Author

I can work on writing some tests for it. I did already confirm that all tests written at the time of the request passed.

@morloderex
Copy link
Contributor

This would be a welcome addition for me 👍

@jeremynikolic
Copy link

jeremynikolic commented Sep 1, 2016

I was hoping to get this feature in 5.3. Thanks for your work man

@milewski
Copy link

I do use a lot of custom pivots... this will make my life easier ~

@pulasthibandara
Copy link

I work with some legacy databases with needs casting on model level. This would be really useful +1

@taylorotwell taylorotwell changed the base branch from 5.3 to master October 6, 2016 17:27
@taylorotwell taylorotwell changed the base branch from master to 5.3 October 6, 2016 17:28

if ($this->using) {
$customPivotClass = $this->using;
$columns = $customPivotClass::getPivotColumns();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you have this new way of specifying which columns to load? Why not just use the traditional belongsToMany->with()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning behind this is that using the traditional method, you would need to defing the columns in every model using the custom pivot. So if you are doing a 3-way pivot, you would have to define the columns in up to 3 different models. That's bad duplication in my mind. The new method allows you to define the columns on the pivot model so you only have to define the columns in one sensible location.

@taylorotwell taylorotwell changed the base branch from 5.3 to master October 6, 2016 17:30
@GrahamCampbell GrahamCampbell changed the title [5.3] Added ability to define custom class to use for pivot models. [5.4] Added ability to define custom class to use for pivot models. Oct 7, 2016
@taylorotwell
Copy link
Member

Is there any interest in continuing this PR?

@pstephan1187
Copy link
Contributor Author

I do want to continue it. I just don't have the time to work on the tests at the moment.

@sisve
Copy link
Contributor

sisve commented Oct 28, 2016

Does this functionality differ from using "normal" models? Will they be called "pivot models" and work differently from existing models?

@pstephan1187
Copy link
Contributor Author

I've added a test. Tests are still new to me, so any direction on additional tests are welcome.

@pstephan1187
Copy link
Contributor Author

@sisve Yes, they differ slightly in that you need to define which columns to load on the pivot model: 13304

@GrahamCampbell
Copy link
Member

Please rebase this on our master. :)

@pstephan1187
Copy link
Contributor Author

@GrahamCampbell Changes have been rebased.

@taylorotwell taylorotwell merged commit 4439576 into laravel:master Nov 9, 2016
@pstephan1187 pstephan1187 deleted the 5.3 branch November 9, 2016 18:49
@taylorotwell
Copy link
Member

I merged this but removed some things to simplify it for now so we can just get the basic spirit of it in. I wanted to get it in the framework at least as a simple starting point.

I removed the automatic eager loading from the constructor because I don't generally think database queries should be done just to new up an object. It makes it hard to new up something for testing, etc.

I also removed the ability to define the attributes to load on the model itself mainly because I feel that is fairly easily recreated by just putting a public static property on the custom pivot model and doing: ->withPivot(PivotModel::$with) or something to that effect. You still aren't having to repeat the actual column names.

Thanks!

@nelson6e65
Copy link

nelson6e65 commented Feb 16, 2017

By the way...

"[...] is fairly easily recreated by just putting a public static property on the custom pivot model and doing: ->withPivot(PivotModel::$with) or [...] " @taylorotwell

...you can't use the static $with property; There is already a non static $with propery defined in Illuminate\Database\Eloquent\Relations\Pivot, for other porpoise.

"Cannot redeclare non static Illuminate\Database\Eloquent\Relations\Pivot::$with as static [...]"


Why are not all available by default as normal models?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants