[英]Symfony Form Entity
我有實體“用戶”和“字段城市”和“國家/地區” ManyToOne,如果用戶輸入個人資料,則創建表格-PersonalInformation,不知道如何將國家/地區帶到城市,然后用戶會下拉列表。現在我有錯誤,現在不知道如何解決了:
Notice: Object of class Proxies\__CG__\PillsBundle\Entity\Country could not be converted to int
實體
/**
* @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;
和城鄉
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;
我創建表格
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';
}
}
國家類別CountryType擴展了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';
}
}
並以表格形式使用
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');
}
和模板:
<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>
和動作:
$formType = new DeveloperPersonalInformationType();
$form = $this->createForm($formType, $developer);
$personalInformationForm = $form->createView();
add()函數的第二個參數必須是formfield-type,例如text或textarea( 請參閱完整詳細信息 )。 我認為您想使用“實體”類型。
$builder
->add('country', 'entity', , array(
'class' => 'PillsBundle:Country',
'choice_label' => 'Select Country',
));
如果“國家/地區”實體對象沒有__toString()方法,則需要choice_label選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.