-
Notifications
You must be signed in to change notification settings - Fork 21
Conversation
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. |
My only other gripe is that I think it would be clearer if scopes used <?php
$store_credits = $current_user->payments->store_credits(); Thoughts? |
My thought re: method vs attribute was keeping consistency with relationships. Though I don't have a strong opinion either way. |
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. |
@lox moved the logic into |
if(isset($this->_scopes[$name])) { | ||
array_unshift($args, $this); | ||
return call_user_func_array($this->_scopes[$name], $args); | ||
} |
There was a problem hiding this comment.
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).
Looks good. Could use a note in the docs, perhaps. |
Looks good. My only final suggestion would be to add a usage example of the |
I'm going to save this for the 1.4 release which I am going to try and get our soon. |
@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. |
Yup, absolutely. Apologies for the delay. |
This creates behavior discussed in #115. Our use case is summed up in:
#115 (comment)