Skip to content

Support for diverge/tree path. #4

@mimol91

Description

@mimol91

I am using this lib to improve performance of my app by reducing number of DB queries.
It wold be great to split features of collector to two methods:

  • collect - current implementation - used to collect associations
  • fetch/load - allows to fetch associations to reduce number of queries. (returns void)

Why to split:
collect, because it has to return results need to accept 'single path' like `car->engine->parts) where each of elements is child of previous one.

fetch in opposite side should allow to handle 'tree' path:

$paths = [
    'engine' => ['parts'],
     'fuel'
];

As a result it will fetch:

  • engine of car (and used parts)
  • fuel type of car

I think that there will not be any way to optimize it (at least I dont know how) , so under the hood it may just do

$collector->collect($car, ['engine', 'parts'];
$collector->collect($car, ['fuel'];

Do you think it may be a 'common/practice' use case, to be worth to add to library?


Classes used in example

class Car
{
    protected $engine;
    private $fuel;
    public function __construct(Engine $engine = null, Fuel $fuel = null)
    {
        $this->engine = $engine;
        $this->fuel = $fuel;
    }
    public function getEngine()
    {
        return $this->engine;
    }
    public function getFuel()
    {
        return $this->fuel;
    }
}

class Engine
{
    public $parts;
    public function __construct(array $parts)
    {
        $this->parts = $parts;
    }
}

class Fuel
{
    protected $name;
    public function __construct(string $name)
    {
        $this->name = $name;
    }
    public function getName(): string
    {
        return $this->name;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions