public function preSetData(DataEvent $event) { $data = $event->getData(); $form = $event->getForm(); // During form creation setData() is called with null as an argument // by the FormBuilder constructor. You're only concerned with when // setData is called with an actual Entity object in it (whether new // or fetched with Doctrine). This if statement lets you skip right // over the null condition. if (null === $data) { return; } // Defaults $defaults = array(); $classTypes = $this->em->createQuery('SELECT u from TSK\\ClassBundle\\Entity\\ClassType u WHERE u.organization=:org')->setParameter('org', $this->org_id)->getResult(); if (count($classTypes)) { foreach ($classTypes as $classType) { $ctc = new ClassTypeCredit(); $ctc->setClassType($classType); $ctc->setClass($data); $ctc->setValue(0); $defaults[$classType->getName()] = $ctc; } } $results = array(); foreach ($data->getClassTypeCredits() as $idx => $ctc) { $results[$ctc->getClassType()->getName()] = $ctc; } $results = array_merge($defaults, $results); $payload = array('type' => 'tsk_class_types', 'label' => ' ', 'required' => false, 'data' => $results); $form->add($this->factory->createNamed('classTypeCredits', 'collection', NULL, $payload)); }
public function reverseTransform($value) { exit; if (null === $value) { return null; } if (!is_array($value)) { throw new UnexpectedTypeException($value, 'array'); } if ('' === implode('', $value)) { return null; } $emptyFields = array(); foreach ($this->fields as $field) { if (!isset($value[$field])) { $emptyFields[] = $field; } } if (count($emptyFields) > 0) { throw new TransformationFailedException(sprintf('The fields "%s" should not be empty', implode('", "', $emptyFields))); } if (isset($value['fk_class_type_id']) && !ctype_digit($value['fk_class_type_id']) && !is_int($value['fk_class_type_id'])) { throw new TransformationFailedException('ClassTypeID is invalid'); } if (isset($value['fk_class_id']) && !ctype_digit($value['fk_class_id']) && !is_int($value['fk_class_id'])) { throw new TransformationFailedException('ClassID is invalid'); } if (isset($value['value']) && !ctype_digit($value['value']) && !is_int($value['value'])) { throw new TransformationFailedException('Value is invalid'); } $classTypeCredit = new ClassTypeCredit(); $classTypeCredit->setClassType(); $classTypeCredit->setClass(); $classTypeCredit->setValue(); }
public function load(ObjectManager $manager) { $em = $this->container->get('doctrine.orm.entity_manager'); foreach ($this->getClasses() as $c) { $ctc = new ClassTypeCredit(); $ctc->setClass($em->getRepository('TSK\\ClassBundle\\Entity\\Classes')->find($c[1])); $ctc->setClassType($em->getRepository('TSK\\ClassBundle\\Entity\\ClassType')->find($c[2])); $ctc->setValue($c[3]); $manager->persist($ctc); } $manager->flush(); }