/** * Hydrate $object with the provided $data. * * @param array $data * @param object $object * @return object */ public function hydrate(array $data, $object) { $fields = $this->metadata; if (isset($data['id'])) { unset($data['id']); } foreach ($data as $fieldName => &$value) { if (isset($fields['referencesMany'][$fieldName])) { $ucFieldName = ucfirst($fieldName); $setter = 'add' . $ucFieldName; $getter = 'get' . $ucFieldName; $remover = 'remove' . $ucFieldName; $refClass = $fields['referencesMany'][$fieldName]['class']; if (!method_exists($object, $setter)) { continue; } foreach ($value as $k => $v) { $value[$k] = $this->mandango->getRepository($refClass)->findOneById($v); } $object->{$remover}($object->{$getter}()->all()); $object->{$setter}($value); continue; } $setter = 'set' . ucfirst($fieldName); if (isset($fields['referencesOne'][$fieldName])) { $refClass = $fields['referencesOne'][$fieldName]['class']; $value = $this->mandango->getRepository($refClass)->findOneById($value); } if (!method_exists($object, $setter)) { continue; } $value = $this->hydrateValue($fieldName, $value); $object->{$setter}($value); } return $object; }
public function testGetRepository() { $mandango = new Mandango($this->metadataFactory, $this->cache); $articleRepository = $mandango->getRepository('Model\\Article'); $this->assertInstanceOf('Model\\ArticleRepository', $articleRepository); $this->assertSame($mandango, $articleRepository->getMandango()); $this->assertSame($articleRepository, $mandango->getRepository('Model\\Article')); $categoryRepository = $mandango->getRepository('Model\\Category'); $this->assertInstanceOf('Model\\CategoryRepository', $categoryRepository); }
public function testCollection() { $mandango = new Mandango($this->metadataFactory, $this->cache); $connection = new Connection($this->uri, $this->dbName . '_collection'); $mandango->setConnection('default', $connection); $mandango->setDefaultConnectionName('default'); $collection = $mandango->getRepository('Model\\Article')->getCollection(); $this->assertEquals($connection->getDatabase()->selectCollection('articles'), $collection); $this->assertSame($collection, $mandango->getRepository('Model\\Article')->getCollection()); }