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

Support for diverge/tree path. #4

Open
mimol91 opened this issue Dec 10, 2017 · 1 comment
Open

Support for diverge/tree path. #4

mimol91 opened this issue Dec 10, 2017 · 1 comment

Comments

@mimol91
Copy link
Contributor

mimol91 commented Dec 10, 2017

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;
    }
}
@malef
Copy link
Contributor

malef commented Dec 20, 2017

This is an idea that I also had in mind, but just didn't get to it yet - so I'm all for it. However you should keep in mind that it will only skip collecting of the leaf entities, i.e. the last association in each path. The preceding entities need to be collected to fetch the next level. Hopefully I will get to implementing it in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants