php - Symfony2: is ArrayCollection better than multiple db queries? -
in symfony2 have 2 entities (companies
, employees
) one-to-many relation (one company can have multiple employees).
each employee object has id
, company_id
(foreign key), badge_number
, last_updated
datetime field, name
, etc
easy, right?
comes tricky part...
each day (from api) $company_upd
array contains list of companies updated of employees data. then, each company in $company_upd
, retrieve api $company_employees_list
array cointains new list of employees .
then loop through array identify new/updated record needs persisted in local database. this:
foreach ($company_employees_list $employee){ $local_employee = $repo->findoneby("badge_number" => $employee["badge_number"]; //if there employee record same badge_number if($local_employee){ //compare last_updated field see if employee record needs updated if($employee["badge_number"] > $local_employee->getlastupdated()){ //update record , persist database $local_employee->setsomething($employee["some_value"]; } } else { //create new employee object , persist database $new_employee = new employee(); $new_employee->setsomething('some_value'); $em->persist($new_employee); } } $em->flush();
of course, approach works fine i'm doing query foreach employee foreach company!! (to retrieve corresponding object , update it)
is possible (and better) use arraycollection? mean: fetch employees arraycollection 1 time , then, work it. search if there employee record specific badge_number , update , persist database? so, basically, did in code above using array_collection instead of separate queries. if new approach faster/better.
if indeed possible (and faster), please write mockup code better understand how manipulate arraycollection , want?
//fetch local db employees arraycollection $employees_ar_coll = $company->getemployees(); //what now???? =)
p.s. api works this, can't change that.
Comments
Post a Comment