Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Adding scopes to collections #116

Merged
merged 7 commits into from
Oct 23, 2014
Merged

Adding scopes to collections #116

merged 7 commits into from
Oct 23, 2014

Conversation

Jud
Copy link
Collaborator

@Jud Jud commented Jun 23, 2014

This creates behavior discussed in #115. Our use case is summed up in:

#115 (comment)

@lox
Copy link
Owner

lox commented Jun 29, 2014

Yeah, I think this is probably the most sane way to do this. Another option I considered was deferring the construction of a Collection instance to the DomainObject and then adding a way to register scopes with that instance:

<?php
class Payment extends DomainObject {
  public static function collection($query, $adder) {
    $col = new Collection(__CLASS__, $query, $adder);
    $col->scope('store_credit', function($chain){ 
       return $chain->filter('payment_type = ?', STORE_CREDIT_TYPE') 
    });
    return $col;
  }
}

That would let you easily change to using a custom Collection instance when the complexity of your scopes got out of control. I suspect your way is simpler.

@lox
Copy link
Owner

lox commented Jun 29, 2014

My only other gripe is that I think it would be clearer if scopes used method() syntax rather than attribute access:

<?php
$store_credits = $current_user->payments->store_credits();

Thoughts?

@Jud
Copy link
Collaborator Author

Jud commented Jun 29, 2014

My thought re: method vs attribute was keeping consistency with relationships. Though I don't have a strong opinion either way.

@lox
Copy link
Owner

lox commented Jun 29, 2014

My only thinking was that it's slightly less magical as a method, then in the future if we support custom collections the calling syntax will stay the same in chains. It's probably a positive that it's visually obvious which is a relationship / attribute vs a scope.

@Jud
Copy link
Collaborator Author

Jud commented Jul 19, 2014

@lox moved the logic into __call.

@lox
Copy link
Owner

lox commented Jul 24, 2014

This is looking good too, nice one @Jud. @harto thoughts?

if(isset($this->_scopes[$name])) {
array_unshift($args, $this);
return call_user_func_array($this->_scopes[$name], $args);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we throw a method-not-found exception in an else block here? Otherwise we'll be silently swallowing errors (or rather, returning null from missing methods).

@harto
Copy link
Collaborator

harto commented Jul 24, 2014

Looks good. Could use a note in the docs, perhaps.

@Jud
Copy link
Collaborator Author

Jud commented Aug 7, 2014

@lox @harto -- that should do it

@harto
Copy link
Collaborator

harto commented Aug 7, 2014

Looks good. My only final suggestion would be to add a usage example of the active scope defined in the README.

@lox
Copy link
Owner

lox commented Sep 4, 2014

I'm going to save this for the 1.4 release which I am going to try and get our soon.

@lox lox added this to the 1.4.0 milestone Sep 4, 2014
@lox lox added the Feature label Sep 4, 2014
@Jud
Copy link
Collaborator Author

Jud commented Sep 4, 2014

@harto @lox updated the readme to add invocation.

@Jud
Copy link
Collaborator Author

Jud commented Oct 21, 2014

@lox would love to at least get this merged in. We've got lots of code we could simplify with scopes and would rather not have to vendor it.

@lox
Copy link
Owner

lox commented Oct 23, 2014

Yup, absolutely. Apologies for the delay.

lox added a commit that referenced this pull request Oct 23, 2014
@lox lox merged commit a08f18f into lox:master Oct 23, 2014
@bjornpost bjornpost modified the milestones: 1.4.0, 2.0.0 May 7, 2016
@bjornpost bjornpost removed this from the 1.4.0 milestone May 7, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants