php - How can I reduce the number of queries Doctrine is performing? -


i'm building product management tool product can have arbitrary number of attributes, documents, features, images, videos single type, brand, , category. there few other related tables, enough demonstrate problem.

there's model class called productmodel contains method (reduced clarity):

  public function loadvalues() {     //product entity data     $this->id = $this->entity->getid();     $this->slug = $this->entity->getslug();      // 1 of each of these     $this->loadtype();     $this->loadbrand();     $this->loadcategory();      // arbitrary number of each of these     $this->loadattributes();     $this->loaddocuments();     $this->loadfeatures();     $this->loadimages();     $this->loadvideos();     ...   } 

each of load methods boiler plate executes method:

  public function loadentitiesbyproductid($productid=0) {      // entities of type associated product.     $entities = $this->entitymanager       ->getrepository($this->entityname)       ->findbyproduct($productid);      $instances = array();      // create model each entity , load data.     foreach ($entities $entity) {       $id = $entity->getid();       $instances[$id] = new $this->childclass();       $instances[$id]->entity = $entity;       $instances[$id]->loadvalues();     }      return $instances;    } 

this ok cases related entity single table, it's mapper. in cases, mapper entities in first query have query related entity within loadvalues() method (via doctrine's get<entity>() method). result of process huge number of queries (often >100). need rid of extraneous queries, i'd without losing idioms i'm using across data models.

is there way entitymanager better job @ using joins group these queries?

there couple problems previous approach:

first, getting entities repository instead of loading them existing entity:

$entities = $this->entitymanager   ->getrepository($this->entityname)   ->findbyproduct($productid); 

better is:

$method = $this->derivegetmethod($this->entityname); $entities = $productentity->$method() 

second, retrieving product entity using $this->entitymanager->getrespository... works fine loading small data sets (a single table or 1 or 2 relations), there's no way repository's findby methods load relations in single query. solution use querybuilder.

$qb = $this->entitymanger->createquerybuilder(); $query = $this->select('product',/*related tables*/)->/*joins etc.*/ $productentity = $query->getsingleresult(); 

Comments

  1. Best 888casino - New York, NY - MapYRO
    Find the best 888casino New York in 전라남도 출장마사지 New York, 화성 출장마사지 NY at Mapyro. See 경상북도 출장샵 locations, hours, 오산 출장마사지 directions, opening 인천광역 출장샵 hours and contact.

    ReplyDelete

Post a Comment

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -