Skip to content

Commit

Permalink
Add support for custom collection classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-redfern committed Sep 12, 2020
1 parent 19cdee8 commit 4e11ba8
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

2020-09-12
----------

* allow collection class to be overridden per model

2020-09-09
----------

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ docblock comment. Additionally: virtual methods can be tagged using `@method`.
__Note:__ to get a raw attribute value, use `->getRawAttribute()`. This will return null if
the attribute is not found, but could also return null for the specified key.

When returning sets of Model objects, the returned set can be customised per model to allow for
specific filters on the collection or other behaviour. Override the `collectionClass` property
with the class name to use. This class must implement the Collection contract from the
somnambulist/collection project and must have `extract()` and `add()` methods.

## More Reading

* [Upgrading from 1.X to 2.0](docs/upgrading_1.X_to_2.0.md)
Expand Down
19 changes: 17 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use JsonSerializable;
use LogicException;
use Somnambulist\Collection\Contracts\Arrayable;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Collection\Contracts\Jsonable;
use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\MutableCollection;
use Somnambulist\Components\AttributeModel\AbstractModel;
use Somnambulist\Components\ReadModels\Exceptions\EntityNotFoundException;
use Somnambulist\Components\ReadModels\Relationships\AbstractRelationship;
Expand Down Expand Up @@ -88,6 +89,15 @@ abstract class Model extends AbstractModel implements Arrayable, Jsonable, JsonS
*/
protected ?string $foreignKey = null;

/**
* The type of collection to return results in when returning multiple results
*
* Allows using custom collections with helper methods for better dealing with sets
* of the same model or for defining filters etc on a loaded relationship. The class
* must implement the Somnambulist\Collection\Contracts\Collection interface.
*/
protected string $collectionClass = MutableCollection::class;

/**
* The relationships to eager load on every query
*/
Expand Down Expand Up @@ -225,6 +235,11 @@ public function new(array $attributes = []): Model
return new static($attributes);
}

public function getCollection(array $models = []): Collection
{
return new $this->collectionClass($models);
}

public function getAttributes(): array
{
$attributes = parent::getAttributes();
Expand Down Expand Up @@ -383,7 +398,7 @@ private function getRelationshipFromMethod(string $method)
}

$relation
->addConstraints($m = new Collection([$this]))
->addConstraints($m = $this->getCollection([$this]))
->addRelationshipResultsToModels($m, $method)
;

Expand Down
7 changes: 4 additions & 3 deletions src/ModelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Pagerfanta\Pagerfanta;
use RuntimeException;
use Somnambulist\Collection\Contracts\Arrayable;
use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Collection\MutableCollection;
use Somnambulist\Components\ReadModels\Contracts\Queryable;
use Somnambulist\Components\ReadModels\Exceptions\EntityNotFoundException;
use Somnambulist\Components\ReadModels\Exceptions\NoResultsException;
Expand Down Expand Up @@ -159,7 +160,7 @@ public function findOrFail($id, ...$columns): Model

public function fetch(): Collection
{
$models = new Collection();
$models = $this->model->getCollection();
$selects = (new FilterGeneratedKeysFromCollection())($this->query->getQueryPart('select'));

if (count($selects) < 1) {
Expand Down Expand Up @@ -508,7 +509,7 @@ public function whereIn(string $column, $values, string $andOr = 'and', bool $no
$values = $values->toArray();
}

$placeholders = Collection::collect($values)
$placeholders = MutableCollection::collect($values)
->map(function ($value) use ($column) {
$this->query->setParameter($key = $this->createParameterPlaceholderKey($column), $value);

Expand Down
2 changes: 1 addition & 1 deletion src/Relationships/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Pagerfanta\Pagerfanta;
use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Components\ReadModels\Contracts\Queryable;
use Somnambulist\Components\ReadModels\Model;
use Somnambulist\Components\ReadModels\ModelBuilder;
Expand Down
2 changes: 1 addition & 1 deletion src/Relationships/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Somnambulist\Components\ReadModels\Relationships;

use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Components\ReadModels\Manager;
use Somnambulist\Components\ReadModels\Model;
use Somnambulist\Components\ReadModels\ModelBuilder;
Expand Down
4 changes: 2 additions & 2 deletions src/Relationships/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Somnambulist\Components\ReadModels\Relationships;

use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Components\ReadModels\Manager;
use Somnambulist\Components\ReadModels\Model;
use Somnambulist\Components\ReadModels\ModelBuilder;
Expand Down Expand Up @@ -65,7 +65,7 @@ public function addRelationshipResultsToModels(Collection $models, string $relat

$entities = $map->all($class, $ids);

$model->setRelationshipValue($relationship, new Collection($entities));
$model->setRelationshipValue($relationship, $this->related->getCollection($entities));
});

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Somnambulist\Components\ReadModels\Relationships;

use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Components\ReadModels\Manager;
use Somnambulist\Components\ReadModels\Model;
use Somnambulist\Components\ReadModels\ModelBuilder;
Expand Down
2 changes: 1 addition & 1 deletion src/Relationships/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Somnambulist\Components\ReadModels\Relationships;

use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Components\ReadModels\Model;
use Somnambulist\Components\ReadModels\ModelBuilder;

Expand Down
4 changes: 2 additions & 2 deletions src/Relationships/HasOneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Somnambulist\Components\ReadModels\Relationships;

use Somnambulist\Collection\MutableCollection as Collection;
use Somnambulist\Collection\Contracts\Collection;
use Somnambulist\Components\ReadModels\Manager;
use Somnambulist\Components\ReadModels\Model;
use Somnambulist\Components\ReadModels\ModelBuilder;
Expand Down Expand Up @@ -64,7 +64,7 @@ public function addRelationshipResultsToModels(Collection $models, string $relat
}
}

$model->setRelationshipValue($relationship, new Collection($entities));
$model->setRelationshipValue($relationship, $this->related->getCollection($entities));
});

return $this;
Expand Down

0 comments on commit 4e11ba8

Please sign in to comment.