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

Basic support for asCollection #12304

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions framework/db/ActiveQuery.php
Expand Up @@ -225,6 +225,10 @@ public function populate($rows)
}
}

if ($this->collectionClass !== null) {
return new $this->collectionClass($models);
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense.

}

return $models;
}

Expand Down
17 changes: 17 additions & 0 deletions framework/db/ActiveQueryTrait.php
Expand Up @@ -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.
Expand All @@ -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)
Copy link
Member

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.

Copy link
Member Author

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.

{
$this->collectionClass = $className;
return $this;
}

/**
* Specifies the relations with which this query should be performed.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/data/ar/CustomerCollection.php
@@ -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);
}
}
10 changes: 10 additions & 0 deletions tests/framework/db/ActiveRecordTest.php
Expand Up @@ -1293,4 +1293,14 @@ public function testUpdateAttributes()
$this->assertTrue($newOrder->getIsNewRecord());
$this->assertEquals($newTotal, $newOrder->total);
}

public function testCollection()
{
$modelClass = 'yiiunit\data\ar\Customer';
$collectionClass = 'yiiunit\data\ar\CustomerCollection';
$customers = Customer::find()->asCollection($collectionClass)->all();
$this->assertInstanceOf($collectionClass, $customers);
$this->assertEquals(3, $customers->count());
$this->assertContainsOnlyInstancesOf($modelClass, $customers);
}
}