I have entity User and field city and country ManyToOne and if user enter for profile I create form - PersonalInformation and dont know how to bring the country to the city and then the user would drop down list.Now I have error and dont now how to solved:
Notice: Object of class Proxies\__CG__\PillsBundle\Entity\Country could not be converted to int
Entity
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @var \PillsBundle\Entity\Cities
*
* @ORM\ManyToOne(targetEntity="\PillsBundle\Entity\Cities")
* @ORM\JoinColumn(name="city_id", referencedColumnName="id", nullable=true)
*/
private $city;
/**
* @var \PillsBundle\Entity\Country
*
* @ORM\ManyToOne(targetEntity="\PillsBundle\Entity\Country")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
*/
private $country;
and City&Country
class Cities
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255)
*/
private $city;
class Country
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=255)
*/
private $country;
I create form
class CityType extends AbstractType
{
private $em;
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
public function getCity()
{
$citiess = $this->em->getRepository('PillsBundle:Cities')->findAll();
$new_cities = array();
foreach($citiess as $citie) {
$new_cities[$citie->getCity()] = $citie->getCity();
}
asort($new_cities);
return $new_cities;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choices' => $this->getCity(),
'multiple' => false,
'required' => false,
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'cities';
}
}
and country class CountryType extends AbstractType { private $em;
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
public function getCountry()
{
$countrys = $this->em->getRepository('PillsBundle:Country')->findAll();
$new_country = array();
foreach($countrys as $country) {
$new_country[$country->getCountry()] = $country->getCountry();
}
asort($new_country);
return $countrys;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choices' => $this->getCountry(),
'multiple' => false,
'required' => false,
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'country';
}
}
and use this type in form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', null, array('label' => 'First Name', 'max_length' => 255, 'required' => false))
->add('lastname', null, array('label' => 'Last Name', 'max_length' => 255, 'required' => false))
->add('email', null, array('label' => 'Email', 'max_length' => 255, 'required' => false))
->add('city', 'cities', array('label' => 'Location','required' => false, 'mapped' => true, 'attr' => array('placeholder' => 'Select Location') ))
->add('country', 'country', array('label' => 'Country','required' => false, 'mapped' => true, 'attr' => array('placeholder' => 'Select Country') ))
->add('skype', null, array('label' => 'Skype', 'max_length' => 255, 'required' => false))
->add('telephone', null, array('label' => 'Phone', 'max_length' => 255, 'required' => false, 'attr' => array('data-inputmask' => "'alias': 'date'")))
->add('save', 'submit');
}
and template:
<div class="form-group">
{{ form_label(infoForm.country, label|default(null), {'label_attr': {'class': 'control-label'}}) }}
{{ form_widget(infoForm.country, {'attr': {'class': 'form-control select2 select2_sample4'}}) }}
</div>
<div class="form-group">
{{ form_widget(infoForm.city, {'attr': {'class': 'form-control input-xlarge select2me'}}) }}
</div>
and action:
$formType = new DeveloperPersonalInformationType();
$form = $this->createForm($formType, $developer);
$personalInformationForm = $form->createView();