Home > Net >  reduce the number of queries with findall
reduce the number of queries with findall

Time:12-10

I want to do a findAll then search for my object in the array according to 1 to several parameters (id, label, etc...) in order to reduce the number of requests to doctrine.

How can I do it? I tried several methods via the PHP documentation but I can't really find what I need. enter image description here

I tested this:

$ontologyAll = $this->ontologyRepository->findAll();
    $toto = array_search(1694, array_column($ontologyAll, 'id'));
    dd($toto);

it returns me false Thank you

CodePudding user response:

findAll() returns Array collection, You can filter useing Criteria

$ontologyAll = $this->ontologyRepository->findAll();
$criteria = Criteria::create()->where(Criteria::expr()->eq('id', 1694));
$ontology = $ontologyAll->matching($criteria);

Or use findFirst method on collection (no idea how fast it is):

$ontology = $ontologyAll->findFirst(function(int $key, $value): bool {
    return $value->id === 1694
});
  • Related