public function setDefaultOptions(OptionsResolverInterface $resolver) { $em = $this->em; $resolver->setDefaults(array('transform_to_id' => false, 'module_likecondition' => false, 'display_method' => 'titleLevelIndented', 'display_transformer' => null, 'choices_afterload' => null, 'empty_value' => '', 'choice_list' => function (Options $options, $previousValue) use($em) { $qb = $em->getRepository('ApplicationIphpCoreBundle:Rubric')->createQueryBuilder('r')->orderBy('r.left'); if ($options['module_likecondition']) { $qb->where($qb->expr()->like('r.controllerName', $qb->expr()->literal($options['module_likecondition']))); } $entityChoiceList = new EntityChoiceList($em, 'Application\\Iphp\\CoreBundle\\Entity\\Rubric', $options['display_method'], new ORMQueryBuilderLoader($qb)); $transformToSimple = $options['transform_to_id']; $displayTransformer = $options['display_transformer']; $choicesAfterLoad = $options['choices_afterload']; if ($displayTransformer) { $transformToSimple = true; } if ($transformToSimple) { $choices = $entityChoiceList->getChoices(); foreach ($choices as $key => $choice) { if (is_object($choice)) { $choices[$key] = $displayTransformer ? $displayTransformer($choice) : $choice->{'get' . $options['display_method']}(); } } if ($choicesAfterLoad) { $choicesAfterLoad($choices); } return new SimpleChoiceList($choices); } else { if ($choicesAfterLoad) { $choicesAfterLoad($entityChoiceList); } return $entityChoiceList; } })); }
public function testNestedChoicesAreManaged() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); // Oh yeah, we're persisting with fire now! $this->em->persist($entity1); $this->em->persist($entity2); $choiceList = new EntityChoiceList($this->em, self::SINGLE_IDENT_CLASS, 'name', null, array('group1' => array($entity1), 'group2' => array($entity2))); $this->assertSame(array('group1' => array(1 => 'Foo'), 'group2' => array(2 => 'Bar')), $choiceList->getChoices()); }
/** * {@inheritdoc} */ public function getChoices() { $choices = parent::getChoices(); if (empty($choices)) { $choices = array(); } $array = array(); foreach ($choices as $value => $label) { $array[] = array('value' => $value, 'label' => $label); } return $array; }
public function setDefaultOptions(OptionsResolverInterface $resolver) { $em = $this->em; $resolver->setDefaults(array('property' => 'title', 'empty_value' => '', 'class' => null, 'root' => null, 'choice_list' => function (Options $options, $previousValue) use($em) { $qb = $em->getRepository($options['class'])->createQueryBuilder('n')->orderBy('n.root')->addOrderBy('n.left'); if ($options['root']) { $qb->where('n.root = :root')->setParameter('root', $options['root']); } $entityChoiceList = new EntityChoiceList($em, $options['class'], $options['property'], new ORMQueryBuilderLoader($qb)); return $entityChoiceList; $choices = $entityChoiceList->getChoices(); foreach ($choices as $key => $choice) { if (is_object($choice)) { $choices[$key] = $choice->{'get' . $options['property']}(); } } return new SimpleChoiceList($choices); })); }
/** * @expectedException Symfony\Component\Form\Exception\FormException */ public function testChoicesMustBeManaged() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); // no persist here! $choiceList = new EntityChoiceList( $this->em, self::SINGLE_IDENT_CLASS, 'name', null, array( $entity1, $entity2, ) ); // triggers loading -> exception $choiceList->getChoices(); }
public function testGroupByInvalidPropertyPathReturnsFlatChoices() { $item1 = new GroupableEntity(1, 'Foo', 'Group1'); $item2 = new GroupableEntity(2, 'Bar', 'Group1'); $this->em->persist($item1); $this->em->persist($item2); $choiceList = new EntityChoiceList($this->em, self::GROUPABLE_CLASS, 'name', null, array($item1, $item2), array(), 'child.that.does.not.exist'); $this->assertEquals(array(1 => $item1, 2 => $item2), $choiceList->getChoices()); }
public function testGroupByInvalidPropertyPathReturnsFlatChoices() { $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); $this->em->persist($item1); $this->em->persist($item2); $choiceList = new EntityChoiceList($this->em, self::ITEM_GROUP_CLASS, 'name', null, array($item1, $item2), 'groupName.child.that.does.not.exist'); $this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $choiceList->getChoices('choices')); }
public function testMinusReplacedByUnderscoreInNegativeIntIds() { $entity1 = new SingleIdentEntity(-1, 'Foo'); $entity2 = new SingleIdentEntity(1, 'Bar'); // Persist for managed state $this->em->persist($entity1); $this->em->persist($entity2); $this->em->flush(); $choiceList = new EntityChoiceList($this->em, self::SINGLE_IDENT_CLASS, 'name'); $this->assertSame(array('_1' => $entity1, 1 => $entity2), $choiceList->getChoices()); $this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2))); $this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1'))); }
public function testSingleNonIntIdFallsBackToGeneration() { $entity1 = new SingleStringIdentEntity('Id 1', 'Foo'); $entity2 = new SingleStringIdentEntity('Id 2', 'Bar'); // Persist for managed state $this->em->persist($entity1); $this->em->persist($entity2); $this->em->flush(); $choiceList = new EntityChoiceList($this->em, self::SINGLE_STRING_IDENT_CLASS, 'name'); $this->assertSame(array(0 => $entity1, 1 => $entity2), $choiceList->getChoices()); }