public function createService(ServiceLocatorInterface $serviceLocator) { // Grab the entity manager. $em = $serviceLocator->get('Doctrine\\ORM\\EntityManager'); // Set up a hydrator $hydrator = new DoctrineHydrator($em); $nullStrategy = new NullStrategy(); $hydrator->addStrategy('estimatedHours', $nullStrategy); $hydrator->addStrategy('actualHours', $nullStrategy); // Create the form. $form = new TaskForm($em); $form->setHydrator($hydrator); return $form; }
public function createService(ServiceLocatorInterface $serviceLocator) { $em = $serviceLocator->get('Doctrine\\ORM\\EntityManager'); $form = new ProjectForm($em); // $form->setInputFilter(new ProjectFilter()); $hydrator = new DoctrineHydrator($em); $nullStrategy = new NullStrategy(); $hydrator->addStrategy('estimatedHours', $nullStrategy); $hydrator->addStrategy('actualHours', $nullStrategy); $hydrator->addStrategy('estimatedCost', $nullStrategy); $hydrator->addStrategy('actualCost', $nullStrategy); $form->setHydrator($hydrator); return $form; }
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 testUsesStrategyOnSimpleFieldsWhenExtractingByReference() { $entity = new Asset\SimpleEntity(); $entity->setId(2); $entity->setField('foo', false); $this->configureObjectManagerForSimpleEntity(); $this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy()); $data = $this->hydratorByReference->extract($entity); $this->assertInstanceOf('DoctrineModuleTest\\Stdlib\\Hydrator\\Asset\\SimpleEntity', $entity); $this->assertEquals(array('id' => 2, 'field' => 'modified while extracting'), $data); }
public function init() { $hydrator = new DoctrineHydrator($this->getObjectManager(), 'Agent\\Entity\\Agent'); $hydrator->addStrategy('updated', new DateTimeStrategy()); $this->setAttribute('method', 'post')->setHydrator($hydrator)->setObject(new Agent()); $dateTime = date('Y-m-d H:i:s'); $this->add(array('type' => 'Zend\\Form\\Element\\Hidden', 'name' => 'id', 'required' => false)); $this->add(array('type' => 'Zend\\Form\\Element\\Hidden', 'name' => 'updated', 'required' => true, 'attributes' => array('value' => $dateTime))); $this->add(array('name' => 'filter', 'type' => 'Agent\\Form\\Fieldset\\FilterFieldset', 'options' => array('label' => "Filters", 'use_as_base_fieldset' => false), 'attributes' => array('id' => 'filter', 'class' => 'collection-fieldset'))); $this->add(array('type' => 'Application\\Form\\Element\\Collection', 'name' => 'criteria', 'required' => false, 'options' => array('column-size' => 'md-12', 'label' => '3. Add Criteria', 'count' => 1, 'should_create_template' => true, 'template_placeholder' => '__index__', 'allow_add' => true, 'allow_remove' => true, 'target_element' => array('type' => 'Agent\\Form\\Fieldset\\AgentCriterionFieldset')), 'attributes' => array('id' => 'criteria', 'class' => 'collection-fieldset criteria-fieldset'))); }
public function testHydrateOneToManyAssociationByReferenceUsingDisallowRemoveStrategy() { // When using hydration by reference, it won't use the public API of the entity to set values (setters) $toMany1 = new Asset\SimpleEntity(); $toMany1->setId(2); $toMany1->setField('foo', false); $toMany2 = new Asset\SimpleEntity(); $toMany2->setId(3); $toMany2->setField('bar', false); $toMany3 = new Asset\SimpleEntity(); $toMany3->setId(8); $toMany3->setField('baz', false); $entity = new Asset\OneToManyEntity(); $this->configureObjectManagerForOneToManyEntity(); // Initally add two elements $entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); // The hydrated collection contains two other elements, one of them is new, and one of them is missing // in the new strategy $data = array('entities' => array($toMany2, $toMany3)); // Use a DisallowRemove strategy $this->hydratorByReference->addStrategy('entities', new Strategy\DisallowRemoveByReference()); $entity = $this->hydratorByReference->hydrate($data, $entity); $entities = $entity->getEntities(false); // DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection $this->assertEquals(3, count($entities)); foreach ($entities as $en) { $this->assertInstanceOf('DoctrineModuleTest\\Stdlib\\Hydrator\\Asset\\SimpleEntity', $en); $this->assertInternalType('integer', $en->getId()); // Only the third element is new so the adder has not been called on it if ($en === $toMany3) { $this->assertNotContains('Modified from addEntities adder', $en->getField(false)); } } $this->assertEquals(2, $entities[0]->getId()); $this->assertSame($toMany1, $entities[0]); $this->assertEquals(3, $entities[1]->getId()); $this->assertSame($toMany2, $entities[1]); $this->assertEquals(8, $entities[2]->getId()); $this->assertSame($toMany3, $entities[2]); }