An open API service indexing awesome lists of open source software.

https://github.com/rekalogika/doctrine-collections-decorator

Lets you easily create decorator classes to modify the behaviors of a Doctrine collection
https://github.com/rekalogika/doctrine-collections-decorator

abstract collection decorator doctrine entity php selectable trait wrapper

Last synced: 6 months ago
JSON representation

Lets you easily create decorator classes to modify the behaviors of a Doctrine collection

Awesome Lists containing this project

README

        

# rekalogika/doctrine-collections-decorator

Lets you easily create decorator classes to dynamically modify the behaviors of
Doctrine Collection objects, including the collection objects used by Doctrine
ORM in your entities.

## Motivation

Custom collection classes won't come to Doctrine ORM anytime soon. Therefore,
the only way to modify the behavior of a Doctrine collection is to use
decorators. However, creating a Collection decorator by hand is a tedious
process.

## Synopsis

The decorator class extending one of our abstract classes:

```php
use Doctrine\Common\Collections\Collection;
use Rekalogika\Collections\Decorator\AbstractCollectionDecorator;

/**
* @extends AbstractCollectionDecorator
*/
class BookCollection extends AbstractCollectionDecorator
{
/**
* @param Collection $collection
*/
public function __construct(private Collection $collection)
{
}

protected function getWrapped(): Collection
{
return $this->collection;
}

// add and override methods here:
// ...
}
```

The usage in an entity:

```php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity()]
class BookShelf
{
/**
* @var Collection
*/
#[ORM\OneToMany(targetEntity: Book::class)]
private Collection $books;

public function __construct()
{
$this->books = new ArrayCollection();
}

public function getBooks(): BookCollection
{
return new BookCollection($this->bookskkk);
}
}
```

## Documentation

[rekalogika.dev/doctrine-collections-decorator](https://rekalogika.dev/doctrine-collections-decorator)

## License

MIT

## Contributing

Issues and pull requests should be filed in the GitHub repository
[rekalogika/doctrine-collections-decorator](https://github.com/rekalogika/doctrine-collections-decorator).