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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,11 @@ trait ActiveQueryTrait | |
*/ | ||
public $asArray; | ||
|
||
/** | ||
* @var string collection class to be instantiated and filled with results. | ||
* @since 2.0.10 | ||
*/ | ||
public $collectionClass; | ||
|
||
/** | ||
* Sets the [[asArray]] property. | ||
|
@@ -42,6 +47,18 @@ public function asArray($value = true) | |
return $this; | ||
} | ||
|
||
/** | ||
* Sets the [[collectionClass]] property. | ||
* @param string $className collection class to be instantiated and filled with results. | ||
* @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 commentThe 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 commentThe 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. |
||
{ | ||
$this->collectionClass = $className; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Specifies the relations with which this query should be performed. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
|
||
namespace yiiunit\data\ar; | ||
|
||
|
||
class CustomerCollection implements \Countable, \IteratorAggregate | ||
{ | ||
private $data; | ||
|
||
public function __construct(array $data) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->data); | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->data); | ||
} | ||
} |
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.
DI should be used here: