【问题标题】:Symfony Form EntitySymfony 表单实体
【发布时间】:2015-11-01 18:13:33
【问题描述】:

我有实体用户和字段城市和国家 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 { 私人 $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();

【问题讨论】:

    标签: php forms symfony doctrine-orm


    【解决方案1】:

    add() 函数的第二个参数必须是表单域类型,例如文本或文本区域 (see full details)。我认为您想使用“实体”类型。

    $builder
        ->add('country', 'entity', , array(
            'class' => 'PillsBundle:Country',
            'choice_label' => 'Select Country',
    ));
    

    如果 Country 实体对象没有 __toString() 方法,则需要 choice_label 选项。

    【讨论】:

    • city 相同。此外,使用这种方法不需要自定义 Type 类。
    • 选项“choice_label”不存在。我将 __toString() 添加到 Country
    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2014-06-08
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多