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 getRemainingViews() { if ($this->ajax) { return array(); } return parent::getRemainingViews(); }
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(); }
/** * {@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 testInitShorthandEntityName() { $item1 = new SingleIntIdEntity(1, 'Foo'); $item2 = new SingleIntIdEntity(2, 'Bar'); $this->em->persist($item1); $this->em->persist($item2); $choiceList = new EntityChoiceList($this->em, 'SymfonyTestsDoctrine:SingleIntIdEntity'); $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); }
public function testPossibleToProvideShorthandEntityName() { $shorthandName = 'SymfonyTestsDoctrine:SingleIdentEntity'; $item1 = new SingleIdentEntity(1, 'Foo'); $item2 = new SingleIdentEntity(2, 'Bar'); $this->em->persist($item1); $this->em->persist($item2); $choiceList = new EntityChoiceList($this->em, $shorthandName, null, null, null, null); $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); }
/** * {@inheritdoc} */ public function getPreferredViews() { if ($this->ajax) { return array(); } return parent::getPreferredViews(); }
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 testMinusReplacedByUnderscoreIfNotLoaded() { $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'); // no getChoices()! $this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2))); $this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1'))); }
public function testGetEmptyArrayChoicesForEmptyValues() { $qb = $this->em->createQueryBuilder()->select('s')->from(self::SINGLE_IDENT_CLASS, 's'); $entityLoader = new ORMQueryBuilderLoader($qb); $choiceList = new EntityChoiceList($this->em, self::SINGLE_IDENT_CLASS, null, $entityLoader); $this->assertEquals(array(), $choiceList->getChoicesForValues(array())); }
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()); }
public function testLegacyInitShorthandEntityName() { $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); $item1 = new SingleIntIdEntity(1, 'Foo'); $item2 = new SingleIntIdEntity(2, 'Bar'); $this->em->persist($item1); $this->em->persist($item2); $choiceList = new EntityChoiceList($this->em, 'SymfonyTestsDoctrine:SingleIntIdEntity'); $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); }