选择类型字段Symfony2

时间:2014-09-10 12:57:15

标签: symfony checkbox doctrine

我尝试在数据库中的字段类别中保存一到三个复选框的值,但是我收到错误:

Notice: Array to string conversion in /var/www/OnTheWay/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 120

字段:

 /**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $category;

获取&设置:

    /**
 * @return mixed
 */
public function getCategory()
{
    return $this->category;
}

/**
 * @param $category
 */
public function setCategory($category)
{
    $this->category[] = $category;
}

个人资料类型:

 namespace Vputi\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ProfileType extends  AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('fio');
        $builder->add('birthDate', null, array('widget' => 'single_text'));
        $builder->add('file');
        $builder->add('yearOnRoad');
        $builder->add('telephone');
        $builder->add('contactMail');
        $builder->add('role', 'choice', array('choices' => array(1 => 'За рулем') ,'expanded'=>true, 'multiple' => true,));
        $builder->add('category', 'choice', array(
            'choices'  => array('A' => 'Категория А', 'B' => 'Категория B', 'C' => 'Категория C',),
            'expanded' => true,
            'multiple' => true,
        ));
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' =>'Vputi\UserBundle\Entity\Profile',
            'cascade_validation' => true,
        ));
    }
 }

这是我的表单类型,我希望你帮助我,并且我省略了getName()方法。

1 个答案:

答案 0 :(得分:0)

问题是$category被定义为string,但您将其用作array

解决方案取决于您想要完成的目标。如果您希望将其映射为array,则必须执行此操作:

/**
 * @ORM\Column(type="array", nullable=true)
 */
private $category;

使用Doctrine的{​​{1}}类型时,请务必考虑到这一点:How to force Doctrine to update array type fields?