4

Hello I try to insert data from the database into a dropdown or ChoiceType. The Data comes from two different databases.

Here is my IndexController:

public function indexAction(Request $request){
 $em = $this->getDoctrine()->getManager();
    //$city = new Cities();->select("c.id,c.active,c.code,t.text name")
 $query = $em->createQueryBuilder()
      ->select("c")
      ->from("DataBaseBundle:Countries","c")
      ->innerJoin("DataBaseBundle:Translationtext","t","WITH","c.translation=t.translation");
 $country = $query->getQuery()->getResult();
 if (!$country){
      throw $this->createNotFoundException("Country not found");
 }
 $form = $this->createForm(CountriesType::class,$country);

 if ($form->isValid()) {
    $user = $form->getData();
    $em->persist($user);
    $em->flush();
}
 return $this->render('ParametersBundle:Countries:index.html.twig', array(
            'form' => $form->createView(),));
     }
}

And my Form called CountriesType:

<?php

namespace ParametersBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class CountriesType extends AbstractType
{
    private $fooChoices = [
        0 => 'choice naught',
        1 => 'choice one',
        2 => 'choice deuce',
    ];
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('active',CheckboxType::class)
        ->add('countries',TextType::class)
        ->add('countries',ChoiceType::class,['choices' => $this->fooChoices,])
        ->add('save', SubmitType::class, array('label' => 'Create Post'));
    }
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'choices' => array(
                'm' => 'Male',
                'f' => 'Female',
            )
            //'data_class' => 'DataBaseBundle\Entity\Cities'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'country';
    }


}

The FooChoices brings data from the code and not from the database.

EDIT: And my Entity (The traslation is in another database called Traslations)

    <?php

namespace DataBaseBundle\Entity;

/**
 * Countries
 */
class Countries
{
    /**
     * @var integer
     */
private $id;

/**
 * @var integer
 */
private $active = '1';

/**
 * @var string
 */
private $code2;

/**
 * @var string
 */
private $code3;

/**
 * @var \DataBaseBundle\Entity\Continents
 */
private $continents;

/**
 * @var \DataBaseBundle\Entity\Translation
 */
private $translation;


/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set active
 *
 * @param integer $active
 *
 * @return Countries
 */
public function setActive($active)
{
    $this->active = $active;

    return $this;
}

/**
 * Get active
 *
 * @return integer
 */
public function getActive()
{
    return $this->active;
}

/**
 * Set code2
 *
 * @param string $code2
 *
 * @return Countries
 */
public function setCode2($code2)
{
    $this->code2 = $code2;

    return $this;
}

/**
 * Get code2
 *
 * @return string
 */
public function getCode2()
{
    return $this->code2;
}

/**
 * Set code3
 *
 * @param string $code3
 *
 * @return Countries
 */
public function setCode3($code3)
{
    $this->code3 = $code3;

    return $this;
}

/**
 * Get code3
 *
 * @return string
 */
public function getCode3()
{
    return $this->code3;
}

/**
 * Set continents
 *
 * @param \DataBaseBundle\Entity\Continents $continents
 *
 * @return Countries
 */
public function setContinents(\DataBaseBundle\Entity\Continents $continents = null)
{
    $this->continents = $continents;

    return $this;
}

/**
 * Get continents
 *
 * @return \DataBaseBundle\Entity\Continents
 */
public function getContinents()
{
    return $this->continents;
}

/**
 * Set translation
 *
 * @param \DataBaseBundle\Entity\Translation $translation
 *
 * @return Countries
 */
public function setTranslation(\DataBaseBundle\Entity\Translation $translation = null)
{
    $this->translation = $translation;

    return $this;
}

/**
 * Get translation
 *
 * @return \DataBaseBundle\Entity\Translation
 */
public function getTranslation()
{


       return $this->translation;
    }
}
4
  • Take a look to my solution and let me know if you face some problems Commented May 12, 2017 at 21:06
  • I made some enhancment take a look no need to use ArrayCollection Commented May 13, 2017 at 11:36
  • Happy to help y. Commented Aug 17, 2017 at 0:00
  • 1
    Could you @Pablo Malynovytch please accept my answer if you think it solved your problem or was the most helpful in finding your solution. Thank you. Cheers! Commented Dec 10, 2018 at 10:05

2 Answers 2

2

Firstable you need to build an array, from the returned query , contains the countries names:

//IndexController
$country = $query->getQuery()->getResult();
    $data= array();
     foreach ( $country as $c) {
        array_push($data,$c->getTranslation());

    }
    $form = $this->createForm(CountriesType::class,$data);

Then you need to pass the options array argument into the add statment.
Just replace this line :

     //CountriesType
   ->add('countries',ChoiceType::class,['choices' => $this->fooChoices,])

with this one :

 ->add('countries',ChoiceType::class,array('choices' => $options))
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for the answer but my Entity has not getName as a function and I tried to put getID but nothing...Only appears unknown information like label_format,by_reference,extra_fields_message,csrf_token_manager
Any idea?I try but nothing
You said "The FooChoices brings data from the code and not from the database" can you tell me what's the data name you wana bring from the database ?(the column name or the attribute name from the entity)
I have in the Database Translationtext the column Text (this i want to print) and Countries Table...There I only have the ID of the translation and the ID (that I want to request as value in the select)...Tell me if u understan or u need the entity of the translation.
The query its correct because I can print it as HTML.
|
0
  <?php
    namespace ParametersBundle\Form;       
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

    class CountriesType extends AbstractType
    {

        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options){
            $fooChoices = $options['choices'];

            $builder->add('active',CheckboxType::class)
            ->add('countries',TextType::class)
            ->add('countries',ChoiceType::class,['choices' => $fooChoices,])
            ->add('save', SubmitType::class, array('label' => 'Create Post'));
        }
        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'choices' => null,
                )
                //'data_class' => 'DataBaseBundle\Entity\Cities'
            ));
        }

        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'country';
        }


    }

And finally, to build the form into the Controller :

$form = $this->createForm(CountriesType::class,array('choices' => $country));

1 Comment

Sorry but nothing...my html :<select id="country_countries" name="country[countries]"></select>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.