private function getVoteInfoAssignment(AbstractTerritoire $territoire) { // try to fetch from cache if (isset($this->cache['voteInfo'][spl_object_hash($territoire)])) { return $this->cache['voteInfo'][spl_object_hash($territoire)]; } // if not in cache, not changed, so we dont mind fetching from db if ($this->voteInfos instanceof PersistentCollection) { $this->voteInfos->setDirty(false); } $criteria = Criteria::create()->where(Criteria::expr()->eq('territoire_id', $territoire->getId())); $collection = $this->voteInfos->matching($criteria); if ($this->voteInfos instanceof PersistentCollection) { $this->voteInfos->setDirty(true); } // filter again if territoire_id were not initialized $criteria = Criteria::create()->where(Criteria::expr()->eq('territoire', $territoire)); $array = array_values($collection->matching($criteria)->toArray()); if (array_key_exists(0, $array)) { $voteInfoAssignment = $array[0]; } // new one if not found if (!isset($voteInfoAssignment)) { $voteInfoAssignment = new VoteInfoAssignment($this, $territoire); $this->voteInfos[] = $voteInfoAssignment; } // put to cache $this->cache['voteInfo'][spl_object_hash($territoire)] = $voteInfoAssignment; return $voteInfoAssignment; }