public function testFindByfilterComponentsDontLookIntheDatabaseItWontHelpYou() { $this->populateA1(); $a1r = $this->entityManager->getRepository('A1'); $a2r = $this->entityManager->getRepository('A2'); $db = $this->entityManager->db; $numQuerys = $db->numQuerys; # Verify the unit tests are using the same db connection as the repos. $a2r->findAll(); $this->assertSame(++$numQuerys, $db->numQuerys); $factory = new FindFilterComponentFactory([PropertyMapperEntityData::class]); # The following shouldn't hit the database - empty container $a1s = $a1r->findByFilterComponents(FindFilterComponentFactory::FIND_ALL, ['foreign_key' => $factory->get('foreign_key', null, new Container())], Repository::ALL); $this->assertSame($numQuerys, $db->numQuerys); return; # container with two non persisted entities $a1s = $a1r->findByFilterComponents(FindFilterComponentFactory::FIND_ALL, array('foreign_key' => FindFilterComponent::factory('foreign_key', null, new Container($a2r->make(), $a2r->make()))), Repository::ALL); $this->assertSame($numQuerys, $db->numQuerys); # entity $a1s = $a1r->findByFilterComponents(FindFilterComponentFactory::FIND_ALL, array('foreign_key' => FindFilterComponent::factory('foreign_key', null, $a2r->make())), Repository::ALL); $this->assertSame($numQuerys, $db->numQuerys); }
/** * Warning! Here be the ->findBy() magic * @return Container */ public function __call($method, $arguments) { // find by if (substr($method, 0, 6) === 'findBy') { $filterFactory = new FindFilterComponentFactory($this->propertyMapper); return $this->findByFilterComponents($filterFactory::FIND_ALL, $filterFactory->initComponentsByString(substr($method, 6), $arguments)); // find one by } elseif (substr($method, 0, 9) === 'findOneBy') { $filterFactory = new FindFilterComponentFactory($this->propertyMapper); return $this->findByFilterComponents($filterFactory::FIND_ONE, $filterFactory->initComponentsByString(substr($method, 9), $arguments)); // remove by } elseif (substr($method, 0, 8) === 'removeBy') { $filterFactory = new FindFilterComponentFactory($this->propertyMapper); $found = $this->findByFilterComponents($filterFactory::FIND_ALL, $filterFactory->initComponentsByString(substr($method, 8), $arguments)); $this->remove($found); return $this; } elseif (substr($method, 0, 6) === 'sortBy') { $property = lcfirst(substr($method, 6)); $direction = count($arguments) > 0 ? $arguments[0] : SORT_ASC; $this->sort($this->generateSortByPropertyClosure($property, $direction)); return $this; } throw new \BadMethodCallException("unknown method {$method}() on " . __CLASS__); }