/** * Edit action * * @return \Zend\Http\Response|ViewModel * @throws \Doctrine\ORM\EntityNotFoundException */ public function editAction() { $form = $this->getEditForm(); $entity = $form->getObject(); if ($this->getRequest()->isPost()) { $data = $this->getRequest()->getPost()->toArray(); $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $hydrator = new DoctrineHydrator($entityManager); $hydrator->hydrate($data, $entity); if ($form->isValid()) { $entityManager->persist($entity); $entityManager->flush(); $authService = new Service\Auth($this->getServiceLocator()); if ($form->get('password')->getValue()) { $authService->generateEquals($entity, $form->get('password')->getValue()); } if (!$this->getRequest()->isXmlHttpRequest()) { return $this->redirect()->toRoute(null, ['controller' => 'management']); } else { return; } } } $viewModel = new ViewModel(['form' => $form]); $viewModel->setTerminal($this->getRequest()->isXmlHttpRequest()); return $viewModel; }
public function testHydrateByValueUsesNamingStrategy() { $this->configureObjectManagerForNamingStrategyEntity(); $name = 'Qux'; $this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy()); $entity = $this->hydratorByValue->hydrate(array('camel_case' => $name), new NamingStrategyEntity()); $this->assertEquals($name, $entity->getCamelCase()); }
/** * @param object $entity * @param array $dadosArray * @return null|object */ public function update($entity = null, $dadosArray = null) { if (!is_null($dadosArray)) { $hydrator = new DoctrineHydrator($this->em); $entity = $hydrator->hydrate($dadosArray, $entity); } $this->em->flush(); return $entity; }
/** * @param array $affiliateData * @return Affiliate */ public function registerNewAffiliate(array $affiliateData) { $hydrator = new DoctrineHydrator($this->getEntityManager()); $affiliate = $hydrator->hydrate($affiliateData, new Affiliate()); $entityManager = $this->getEntityManager(); $entityManager->persist($affiliate); $entityManager->flush(); return $affiliate; }
public function testHydratingObjectsWithStrategy() { $data = array('username' => 'foo', 'password' => 'bar'); $modifiedData = array('username' => 'MODIFIED', 'password' => 'bar'); $this->hydrator->addStrategy('username', new TestAsset\HydratorStrategy()); $object = $this->hydrator->hydrate($data, new stdClass()); $extract = $this->hydrator->extract($object); $this->assertEquals($modifiedData, $extract); }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Contact\\Entity\\OptIn'); $this->optIn = $hydrator->hydrate($this->optInData, new OptIn()); $this->entityManager->persist($this->optIn); $this->entityManager->flush(); $this->assertInstanceOf('Contact\\Entity\\OptIn', $this->optIn); $this->assertNotNull($this->optIn->getId()); $this->assertEquals($this->optIn->getOptIn(), $this->optInData['optIn']); }
public function save($data) { $entityManager = $this->getEntityManager(); /** @var DoctrineHydrator $hydrator */ $hydrator = new DoctrineHydrator($entityManager, 'Authorization\\Entity\\Provider'); $provider = $hydrator->hydrate($data, new Provider()); $entityManager->persist($provider); $entityManager->flush(); return $provider; }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Contact\\Entity\\AddressType'); $this->addressType = $hydrator->hydrate($this->addressTypeData, new AddressType()); $this->entityManager->persist($this->addressType); $this->entityManager->flush(); $this->assertInstanceOf('Contact\\Entity\\AddressType', $this->addressType); $this->assertNotNull($this->addressType->getId()); $this->assertEquals($this->addressType->getType(), $this->addressTypeData['type']); }
public function hydrate(array $data, $object) { $this->prepare($object); $plugins = $this->getPlugins(); /* @var $plugin HydratorPluginInterface */ foreach ($plugins as $plugin) { $data = $plugin->hydrate($object, $data); } return parent::hydrate($data, $object); }
public function testHandleDateTimeConversionUsingByValue() { // When using hydration by value, it will use the public API of the entity to set values (setters) $entity = new Asset\SimpleEntityWithDateTime(); $this->configureObjectManagerForSimpleEntityWithDateTime(); $now = time(); $data = array('date' => $now); $entity = $this->hydratorByValue->hydrate($data, $entity); $this->assertInstanceOf('DateTime', $entity->getDate()); $this->assertEquals($now, $entity->getDate()->getTimestamp()); }
public function testUsesStrategyOnSimpleFieldsWhenHydratingByReference() { // When using hydration by value, it will use the public API of the entity to set values (setters) $entity = new Asset\SimpleEntity(); $this->configureObjectManagerForSimpleEntity(); $data = array('field' => 'foo'); $this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy()); $entity = $this->hydratorByReference->hydrate($data, $entity); $this->assertInstanceOf('DoctrineModuleTest\\Stdlib\\Hydrator\\Asset\\SimpleEntity', $entity); $this->assertEquals('modified while hydrating', $entity->getField(false)); }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Program\\Entity\\Roadmap'); $this->roadmap = $hydrator->hydrate($this->roadmapData, new Roadmap()); $this->entityManager->persist($this->roadmap); $this->entityManager->flush(); $this->assertInstanceOf('Program\\Entity\\Roadmap', $this->roadmap); $this->assertNotNull($this->roadmap->getId()); $this->assertSame($this->roadmapData['roadmap'], $this->roadmap->getRoadmap()); $this->assertSame($this->roadmapData['description'], $this->roadmap->getDescription()); $this->assertSame($this->roadmapData['dateReleased'], $this->roadmap->getDateReleased()); }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Contact\\Entity\\Web'); $this->web = $hydrator->hydrate($this->webData, new Web()); $this->entityManager->persist($this->web); $this->entityManager->flush(); $this->assertInstanceOf('Contact\\Entity\\Web', $this->web); $this->assertNotNull($this->web->getId()); $this->assertNotNull($this->web->getResourceId()); $this->assertEquals($this->web->getContact()->getId(), $this->webData['contact']->getId()); $this->assertEquals($this->web->getWeb(), $this->webData['web']); }
/** * Update an existing resource * * @param mixed $id * @param mixed $data * @throws \Exception * @return mixed */ public function update($id, $data) { $indicateurRepository = $this->getIndicsUsersRepository(); $indicUser = $indicateurRepository->find($id); if (empty($indicUser)) { throw new \Exception('Aucun enregistrement trouvé pour cet id : ' . $id); } $hydrator = new DoctrineHydrator($this->entityManager); $indicUser = $hydrator->hydrate($data, $indicUser); $this->entityManager->flush(); return new JsonModel(array("data" => $data)); }
public function create($data) { var_dump($data); $hydrator = new DoctrineObject($this->objectManager, 'Application\\Entity\\Credentials'); $person = new Person(); $credentials = new Credentials(); $credentials->setOwner($person); $hydrator->hydrate($data, $credentials); $person->addCredential($credentials); $this->objectManager->persist($person); $this->objectManager->flush(); return $person; }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'General\\Entity\\Eu'); $this->eu = $hydrator->hydrate($this->euData, new Eu()); $this->entityManager->persist($this->eu); $this->entityManager->flush(); //Since we don't save, we give the $eu a virtual id $this->eu->setId(1); $this->assertInstanceOf('General\\Entity\\Eu', $this->eu); $this->assertNotNull($this->eu->getId()); $this->assertEquals($this->eu->getSince(), $this->euData['since']); $this->assertEquals($this->eu->getCountry()->getCountry(), $this->euData['country']->getCountry()); }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Program\\Entity\\Domain'); $this->domain = $hydrator->hydrate($this->domainData, new Domain()); $this->entityManager->persist($this->domain); $this->entityManager->flush(); $this->assertInstanceOf('Program\\Entity\\Domain', $this->domain); $this->assertNotNull($this->domain->getId()); $this->assertSame($this->domainData['domain'], $this->domain->getDomain()); $this->assertSame($this->domainData['roadmap']->getId(), $this->domain->getRoadmap()->getId()); $this->assertSame($this->domainData['description'], $this->domain->getDescription()); $this->assertSame($this->domainData['color'], $this->domain->getColor()); $this->assertSame($this->domainData['mainId'], $this->domain->getMainId()); }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Contact\\Entity\\Email'); $this->email = $hydrator->hydrate($this->emailData, new Email()); $this->entityManager->persist($this->email); $this->entityManager->flush(); $this->assertInstanceOf('Contact\\Entity\\Email', $this->email); $this->assertNotNull($this->email->getId()); $this->assertNotNull($this->email->getDateCreated()); $this->assertNotNull($this->email->getDateUpdated()); $this->assertEquals($this->email->getContact()->getId(), $this->emailData['contact']->getId()); $this->assertEquals($this->email->getEmail(), $this->emailData['email']); $this->assertNotNull($this->email->getResourceId()); }
/** * @return ViewModel */ public function createAction() { $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $repository = $entityManager->getRepository('Categories\\Entity\\Categories'); /** @var \Categories\Service\Categories $categoriesService */ $categoriesService = $this->getServiceLocator()->get('Categories\\Service\\Categories'); $form = $this->getCreateForm(); $parentId = !$this->params()->fromRoute('parentId') ? null : $this->params()->fromRoute('parentId'); $urlHelper = $this->getServiceLocator()->get('viewhelpermanager')->get('url'); $form->setAttribute('action', $urlHelper('categories/create', ['parentId' => $parentId])); if ($this->getRequest()->isPost()) { $form->setInputFilter(new Filter\CreateInputFilter($this->getServiceLocator())); $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { $aliasValid = new Validator\NoObjectExists(['object_repository' => $repository, 'fields' => ['alias', 'parentId']]); if ($aliasValid->isValid(['alias' => $form->get('alias')->getValue(), 'parentId' => $parentId])) { $category = $this->getEntity(); $category->setParentId(!$parentId ? null : $repository->find($parentId)); $category->setOrder($this->getMaxOrder($parentId)); $hydrator = new DoctrineHydrator($entityManager); $hydrator->hydrate($form->getData(), $category); $entityManager->persist($category); $entityManager->flush(); //Add image from session if ($categoriesService->ifImagesExist()) { $imageService = $this->getServiceLocator()->get('Media\\Service\\File'); foreach ($categoriesService->getSession()->ids as $imageId) { $imageService->associateFileWithObject($this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager')->getRepository('Media\\Entity\\File')->find($imageId), $category); } $categoriesService->clearImages(); } $this->flashMessenger()->addSuccessMessage('Category has been successfully created!'); if (!$this->getRequest()->isXmlHttpRequest()) { return $this->redirect()->toRoute('categories/default', array('controller' => 'management', 'action' => 'index')); } else { return; } } else { $form->get('alias')->setMessages(array('errorMessageKey' => 'Alias must be unique in it\'s category!')); } } } else { $categoriesService->clearImages(); } $imageService = new File($this->getServiceLocator()); $viewModel = new ViewModel(); $viewModel->setVariables(['form' => $form, 'fileUpload' => ['imageService' => $imageService, 'module' => 'image-categories', 'type' => \Media\Entity\File::IMAGE_FILETYPE, 'id' => null]]); $viewModel->setTerminal($this->getRequest()->isXmlHttpRequest()); return $viewModel; }
public function testCanSaveEntityInDatabase() { $hydrator = new DoctrineObject($this->entityManager, 'Contact\\Entity\\Address'); $this->address = $hydrator->hydrate($this->addressData, new Address()); $this->entityManager->persist($this->address); $this->entityManager->flush(); $this->assertInstanceOf('Contact\\Entity\\Address', $this->address); $this->assertNotNull($this->address->getId()); $this->assertEquals($this->address->getContact()->getId(), $this->addressData['contact']->getId()); $this->assertEquals($this->address->getAddress(), $this->addressData['address']); $this->assertEquals($this->address->getZipcode(), $this->addressData['zipcode']); $this->assertEquals($this->address->getCity(), $this->addressData['city']); $this->assertEquals($this->address->getCountry()->getId(), $this->addressData['country']->getId()); $this->assertNotNull($this->address->getResourceId()); }
/** * Load users. * * @param ObjectManager $manager Object manager instance. * * @return void */ public function load(ObjectManager $manager) { $users = $this->getUsersToPopulate(); $hydrator = new DoctrineHydrator($manager); echo 'Creating users..', PHP_EOL; $i = 0; foreach ($users as $item) { $user = $hydrator->hydrate($item, new UserEntity()); $manager->persist($user); echo '.'; $i++; } $manager->flush(); echo PHP_EOL, 'Total of ', $i, ' users created.', PHP_EOL; }
public function addAction() { $request = $this->getRequest(); if ($this->_isXmlHttpRequestViaPost()) { $em = $this->getEntityManager(); $entity = new CalendarWorkItem(); $start = new \DateTime(); $start->setTimestamp(strtotime($this->params()->fromPost('start'))); // Hydration $hydrator = new DoctrineHydrator($em); $hydrator->hydrate(array('name' => $this->params()->fromPost('title'), 'startAt' => $start, 'user' => $this->getCurrentUser(), 'calendar' => $this->getCurrentUser()->getCalendar()), $entity); $em->persist($entity); $em->flush(); return new JsonModel(array('id' => $entity->getId())); } }
/** * @param array $memberData * @return Member */ public function registerNewMember(array $memberData) { $hydrator = new DoctrineHydrator($this->getEntityManager()); $member = new Member(); $member->setServiceLocator($this->getServiceLocator()); $member = $hydrator->hydrate($memberData, $member); $entityManager = $this->getEntityManager(); $entityManager->persist($member); $entityManager->flush(); $mailService = $this->getMailService(); $message = $mailService->prepareMessage('signup', array('member' => $member)); $message->addTo($member->getEmail(), $member->getName()); $message->setSubject('USCSS New Member Confirmation'); $mailService->send($message); return $member; }
/** * Create a new resource * * @param mixed $data * @return mixed */ public function create($data) { $matches = $this->getEvent()->getRouteMatch(); $params = $matches->getParams(); $author = $this->dm->getRepository('User\\Document\\User')->findOneById($params['uid']); $hydrator = new DoctrineHydrator($this->dm); $form = new MessageForm(); $form->setData($data); if ($form->isValid()) { $message = new Message($author); $newMessage = $hydrator->hydrate($form->getData(), $message); $this->dm->persist($newMessage); $this->dm->flush(); return new JsonModel(['Success' => ['message' => 'Your message was created successfully!', '_link' => $this->url()->fromRoute('api.user.message') . $newMessage['id']]]); } else { return new JsonModel(['Error' => ['message' => 'Invalid Request Object!', 'request' => $data]]); } }
/** * set option * * @param $key * @param $value * @param string $namespace * @param null $description * @throws \Exception */ public function setOption($key, $value, $namespace = \Options\Entity\Options::NAMESPACE_DEFAULT, $description = null) { $data = array('key' => $key, 'namespace' => $namespace, 'value' => $value, 'description' => $description); $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); /** * @var \Options\Entity\Options $option */ $option = $this->getServiceLocator()->get('Options\\Entity\\Options'); $objectManager->getConnection()->beginTransaction(); try { $hydrator = new DoctrineHydrator($objectManager); $hydrator->hydrate($data, $option); $option->setCreated(new \DateTime(date('Y-m-d H:i:s'))); $option->setUpdated(new \DateTime(date('Y-m-d H:i:s'))); $objectManager->persist($option); $objectManager->flush(); $objectManager->getConnection()->commit(); } catch (\Exception $e) { $objectManager->getConnection()->rollback(); throw $e; } }
/** * {@inheritdoc} */ public function hydrate(array $data, $object) { // get collection properties $collectionProperties = []; foreach ($data as $name => $values) { $computedFieldName = $this->computeHydrateFieldName($name); if ($this->hasStrategy($computedFieldName) && $this->getStrategy($computedFieldName) instanceof DoctrineCollectionStrategy) { $collectionProperties[$computedFieldName] = $values; unset($data[$name]); } } // hydrate object $object = parent::hydrate($data, $object); // hydrate collection properties using strategies foreach ($collectionProperties as $name => $collectionValues) { $method = 'set' . ucfirst($name); $object->$method($this->getStrategy($name)->hydrate($collectionValues)); } return $object; }
/** * Creates new user. * * @param array $userData * @return \User\Entity\User */ protected function createUser(array $userData = null, $role = null) { !$userData ? $data = $this->userData : ($data = $userData); $objectManager = $this->getApplicationServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $user = new Entity\User(); $objectManager->getConnection()->beginTransaction(); $hydrator = new DoctrineHydrator($objectManager); $hydrator->hydrate($data, $user); $user->setDisplayName($user->getEmail()); $user->setRole($user::ROLE_USER); $user->setConfirm($user->generateConfirm()); $user->setStatus($user::STATUS_ACTIVE); if ($role == $user::ROLE_ADMIN) { $user->setRole($user::ROLE_ADMIN); } $objectManager->persist($user); $objectManager->flush(); /** @var $authService \User\Service\Auth */ $authService = $this->getApplicationServiceLocator()->get('User\\Service\\Auth'); $authService->generateEquals($user, $data['password']); $objectManager->getConnection()->commit(); return $user; }
/** * Performs an authentication attempt * * @return \Zend\Authentication\Result * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface If authentication cannot be performed */ public function authenticate() { $em = $this->getEntityManager(); $repo = $em->getRepository($this->entityName); $identity = $this->getIdentity(); $userObject = $repo->findOneBy(array('username' => $identity)); if (!$userObject) { $authCode = AuthResult::FAILURE_IDENTITY_NOT_FOUND; $messages = array('A record with the supplied identity could not be found.'); return new AuthResult($authCode, $identity, $messages); } $bcrypt = new Bcrypt(); $bcrypt->setCost(14); if (!$bcrypt->verify($this->getCredential(), $userObject->getPassword())) { // Password does not match $messages = array('Supplied credential is invalid.'); $authCode = AuthResult::FAILURE_CREDENTIAL_INVALID; return new AuthResult($authCode, $identity, $messages); } $userObject->setName('IdAuth\\Adapter\\Doctrine'); $hydrator = new DoctrineObject($em, $this->entityName); $hydrator->hydrate($userObject->getRoles(), $userObject); return new AuthResult(AuthResult::SUCCESS, $userObject, array('Authentication Successful')); }
/** * {@inheritDoc} */ public function hydrate(array $data, $object) { $this->prepare($object); $data = $this->hydrateViaPlugins($data, $object); return parent::hydrate($data, $object); }
/** * Creates new category. * * @param $categoryData * @return \Categories\Entity\Categories */ public function createCategory($categoryData) { /** * @var \Doctrine\ORM\EntityManager $objectManager */ $objectManager = $this->getApplicationServiceLocator()->get('Doctrine\\ORM\\EntityManager'); // $category = $this->getApplicationServiceLocator()->get('Categories\Entity\Categories'); $category = new \Categories\Entity\Categories(); $objectManager->getConnection()->beginTransaction(); $hydrator = new DoctrineHydrator($objectManager); $hydrator->hydrate($categoryData, $category); $objectManager->persist($category); $objectManager->flush(); $objectManager->getConnection()->commit(); $objectManager->clear(); return $category; }