0

I try to save values of one - three checkboxes in field category in database, but i get the error :

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

The field:

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

Get & Set:

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

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

Profile type:

 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,
        ));
    }
 }

Here is my form type, I hope you help me, and iam ommit getName() method.

1 Answers1

0

The problem is $category is defined as a string but you're using it like an array.

The solution depends on exactly what you want to accomplish. If you want it to be mapped as an array you have to do this:

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

When using Doctrine's array type, make sure you take this into account: How to force Doctrine to update array type fields?

Community
  • 1
  • 1
FyodorX
  • 1,490
  • 2
  • 19
  • 23
  • thats hepl on registration, but i get another problem on edit profile: `Notice: Array to string conversion in /var/www/OnTheWay/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 462` –  Sep 10 '14 at 14:57
  • Can you post your `Type`? I'll update my answer based on that. – FyodorX Sep 10 '14 at 15:02
  • yes. but i dont get any results, problem still here –  Sep 11 '14 at 09:15