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

Add custom collection support #273

Merged
merged 1 commit into from
Oct 19, 2015

Conversation

klaude
Copy link
Contributor

@klaude klaude commented Oct 19, 2015

Add support for models that override Eloquent's newCollection() method with a custom collection type. Do this by executing the relationship on the model then executing that relationship's newCollection() method to see what type of collection class is returned.

This also removes the the now-unused Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getClassName() method since relationship properties aren't determined with as much string logic as before.

This change has one side-effect. Since it determines relationship property types by executing the relationship it now puts fully-qualified model class names into the classes' docblock tags instead of class alias names or class names without their namespace if the model and its related model are in the same namespace. For instance, what may have been rendered as:

use My\Related\SingularRelationship as Singular;
use My\Related\MultipleRelationship;
use My\Related\RelationshipWithACustomCollection; // Assume this uses a \My\CustomCollection collection type.

/**
 * ...
 * @property-read Singular $singular
 * @property-read \Illuminate\Database\Eloquent\Collection|MultipleRelationship[] $multiple
 * @property-read \Illuminate\Database\Eloquent\Collection|RelationshipWithACustomCollection[] $custom
 */

is now:

use My\Related\SingularRelationship as Singular;
use My\Related\MultipleRelationship;
use My\Related\RelationshipWithACustomCollection; // Assume this uses a \My\CustomCollection collection type.

/**
 * ...
 * @property-read \My\Related\SingularRelationship $singular
 * @property-read \Illuminate\Database\Eloquent\Collection|\My\Related\MultipleRelationship[] $multiple
 * @property-read \My\CustomCollection|\My\Related\RelationshipWithACustomCollection[] $custom
 */

Note how every type hint is now fully qualified and unaliased, but it uses the custom collection class. It's a little wordier, but it's still readable by IDEs. I've tested this out on some of my Laravel projects and haven't seen any other side effects. Let me know what you think!

Add support for models that override Eloquent's newCollection() with a
custom collection type. Do this by executing the relationship on the
model then executing that relationship's newCollection() method to see
what type of colletion class is returned.

Remove the now-unused
Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getClassName() method.
@shehi
Copy link

shehi commented Oct 19, 2015

Is this for NoSQL support? I really look forward to this implementation.

barryvdh added a commit that referenced this pull request Oct 19, 2015
@barryvdh barryvdh merged commit 11037ac into barryvdh:master Oct 19, 2015
barryvdh added a commit that referenced this pull request Oct 19, 2015
@barryvdh
Copy link
Owner

Changed it a bit with 56f8950
Removed some left-over code, checked if it's an actual model.

@klaude
Copy link
Contributor Author

klaude commented Oct 19, 2015

@shehi, it adds support for models that return custom collections via newCollection().

http://laravel.com/docs/5.1/eloquent-collections#custom-collections

Thanks, @barryvdh!

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.

3 participants