Skip to content

Create Object specific collections behavior #17

@wollanup

Description

@wollanup

(Maybe in another repo)

It's better to use typed collection parameter (UserCollection) rather generic ObjectCollection

In Query classes, override defaultFormatterClass property :

    protected $defaultFormatterClass = ModelCriteria::FORMAT_OBJECT;

with our new formatter :

    protected $defaultFormatterClass = UserFormatter::class;

Create a collection class :

<?php
use Domain\Core\User\User;
use Propel\Runtime\Collection\ObjectCollection;
/**
 * Class UserCollection
 */
class UserCollection extends ObjectCollection
{
    /**
     * UserCollection constructor.
     *
     * @param array $data
     */
    public function __construct(array $data = [])
    {
        $this->setModel(User::class);
        parent::__construct($data);
    }
}

Then create an Object Formatter like this :

<?php
use Domain\Core\User\Collection\UserCollection;
use Propel\Runtime\Formatter\ObjectFormatter;

class UserFormatter extends ObjectFormatter
{
    public function getCollectionClassName()
    {
        return UserCollection::class;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions