public function test__construct() { $metadata = new ClassMetadata(__NAMESPACE__ . '\\ModelTest'); $metadata->addModel('entity', __NAMESPACE__ . '\\EntityTest'); $metadata->setIdentifier('entity', 'id'); $metadataFactory = $this->getMock('Pok\\PoolDBM\\Mapping\\ClassMetadataFactory', array('getMetadataFor', 'setModelManager')); $metadataFactory->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata)); $refl = new \ReflectionClass('Pok\\PoolDBM\\Manager\\BaseManager'); $this->assertTrue($refl->isInstantiable()); $pool = new Pool(); $pool->addManager('entity', new EntityManager()); $manager = new TestManager(__NAMESPACE__ . '\\ModelTest', new ModelManager($pool, $metadataFactory)); $this->assertInstanceOf('Pok\\PoolDBM\\ModelRepository', $manager->getRepository($manager)); $this->assertInstanceOf(__NAMESPACE__ . '\\ModelTest', $manager->create()); $manager->save(new ModelTest()); $manager->save(new ModelTest(), true); try { $manager->save(new \stdClass()); } catch (\RuntimeException $e) { $this->assertEquals('Manager "Pok\\PoolDBM\\Tests\\Manager\\TestManager" is unable to save model "stdClass"', $e->getMessage()); } $manager->clear(); $this->assertInstanceOf(__NAMESPACE__ . '\\ModelTest', $manager->find(null)); $this->assertEquals(1, count($manager->findBy(array()))); $this->assertInstanceOf(__NAMESPACE__ . '\\ModelTest', $manager->findOneBy(array())); $this->assertEquals(1, count($manager->findAll())); }
public function testGetMetadata() { $mockDriver = new MetadataDriverMock(); $manager = $this->createModelManager($mockDriver); $classFactory = $manager->getMetadataFactory(); $class = new ClassMetadata('TestMultiModel\\User'); $class->addModel('entity', 'Entity\\User', array('name')); $class->addModel('document', 'Document\\User', array('profileContent')); $class->setIdentifier('document', 'id'); $classFactory->setMetadataFor($class->getName(), $class); $class = $classFactory->getMetadataFor('TestMultiModel\\User'); $this->assertTrue($class->hasIdentifier()); $this->assertEquals(array('name', 'profileContent'), $class->getFieldNames()); }
/** * Loads a list of model by a list of field criteria. * * @param array $criteria * @param array $orderBy * @param integer $limit (optional) * @param integer $offset (optional) * * @return array */ public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null) { $pool = $this->manager->getPool(); $manager_id = $this->class->getManagerIdentifier(); $models = array(); foreach ($this->class->getFieldManagerNames() as $manager) { $models[$manager] = $this->class->getFieldMapping($manager)->getName(); } $data = $pool->getManager($manager_id)->getRepository($models[$manager_id])->findBy($criteria, $orderBy, $limit, $offset); if (empty($data)) { return array(); } $builder = new ModelBuilder($this->manager, $this->uow, $this->class); return $builder->buildAll($data, $manager_id); }
/** * @return array */ public function getManagers() { return $this->metadata->getFieldManagerNames(); }
/** * Performed research on the model via their repository. * * @param ClassMetadata $class * @param string $manager * @param array $ids * @param callable $stacker */ public function loaderModels(ClassMetadata $class, $manager, array $ids, \Closure $stacker) { $classOfManagerName = $class->getFieldMapping($manager); $pool = $this->manager->getPool(); $methodFind = $classOfManagerName->getRepositoryMethod(); $repository = $pool->getManager($manager)->getRepository($classOfManagerName->getName()); if ($methodFind && method_exists($repository, $methodFind)) { foreach ($repository->{$methodFind}($ids) as $object) { $id = $class->getIdentifierValue($object); $stacker($id, $object); } } else { trigger_error(sprintf('findOneBy in ModelPersister::loadAll context is depreciate. Define repository-method for "%s" manager model, see mapping for "%s".', $manager, $class->getName()), E_USER_DEPRECATED); $repository = $pool->getManager($manager)->getRepository($classOfManagerName->getName()); $field = $class->getIdentifierReference($manager)->field; foreach ($ids as $id) { $object = $repository->findOneBy(array($field => $id)); if (!$object) { continue; } $id = $class->getIdentifierValue($object, $manager); $stacker($id, $object); } } }
/** * Multiple hydration model. * * @param array $objects * @param array $fields List of fields prime (optional) * * @return array */ public function hydrate(array $objects, array $fields = array()) { return $this->modelBuilder->buildAll($objects, $this->class->getManagerIdentifier(), $fields); }
/** * @depends testFieldMappings * @param ClassMetadata $class */ public function testIdentifier($class) { $this->assertEquals('id', $class->getFieldIdentifier()); $this->assertEquals('document', $class->getManagerIdentifier()); return $class; }
/** * @param ClassMetadataInfo $class * @param \SimpleXMLElement $model * * @throws \InvalidArgumentException When the definition cannot be parsed */ protected function addModel(ClassMetadataInfo $class, \SimpleXMLElement $model) { $fields = array(); foreach ($model as $field) { /** @var \SimpleXMLElement $field */ if ('field' === $field->getName()) { $fields[] = (string) $field['name']; } else { throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $field->getName())); } } $class->addModel((string) $model['manager'], (string) $model['name'], $fields, isset($model['repository-method']) ? (string) $model['repository-method'] : null); }