-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Basic support for asCollection #12304
Conversation
samdark
commented
Aug 24, 2016
Q | A |
---|---|
Is bugfix? | no |
New feature? | yes |
Breaks BC? | no |
Tests pass? | yes |
Fixed issues | #10806 |
@@ -225,6 +225,10 @@ public function populate($rows) | |||
} | |||
} | |||
|
|||
if ($this->collectionClass !== null) { | |||
return new $this->collectionClass($models); |
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.
I'd suggest to move this line into a separate protected method. Then it could be overridden with custom logic.
Use case: I want to pass original Query
object in my collection as a 2nd argument of the constructor.
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.
Makes sense.
Are we going to implement |
No since we aren't using collections. |
* @return object collection instance. | ||
* @since 2.0.10 | ||
*/ | ||
protected function createCollection($class, $models) |
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.
IMO $models
is enough. Method can access $this->collectionClass
by itself
I suppose we should create a |
* @return $this the query object itself | ||
* @since 2.0.10 | ||
*/ | ||
public function asCollection($className) |
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.
See #12304 (comment)
$className should be optional here, while default collection class is used.
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.
Default is no collection at all. It doesn't make sense to introduce any default collection since the only thing you can achieve w/ collection that you can't achieve otherwise is type safety.
Nope. First of all, it will prevent using external collection implementations such as Laravel's. Second, there's no requirements except that first argument of the constructor is an array of data. |
As I already said then there is no sense to introduce the entire feature. What we are saving here? $collection = User::find()->asCollection('UserCollection')->all(); insead of: $collection = new UserCollection(User::find()->all()); Make absolutely no sense to me. Also what do you mean by One more thing how this feature should behave for the |
Also collections should be introduced at the level of the |
The implementation here is incomplete proof of concept. You're absolutely correct that primary collection could be filled outside of AR and it's preferred to do so. The difference should be in handling relations... By type safety I mean in an interface you'll be able to do: interface X
{
public function doit(PostCollection $posts);
}
// instead of
interface X
{
public function doit(array $posts);
} |
|
In which way it will be different?
So how the default collection class existance contradicts this ability? It is like telling the existance of the interface X
{
public function doit(PostActiveRecord $post);
} while it provides more flexisbility to write following type restiction: interface X
{
public function doit(BaseActiveRecord $model);
}
In this case
Note that |
Well, relations could be automatically wrapped into collections. |
By the way, what about I agree with @cebe here: #10806 (comment) The collection functionality in Yii is split into different classes, providing all necessay support for most common features. |
For what purpose? Just to 'eat' more memory? |
For the linking improvement we should use
|
OK. I agree with most of your arguments. Seems it's better to approach it differently via enhancing |