Symfony表单实体ManyToMany

时间:2015-11-10 10:02:01

标签: php forms symfony doctrine-orm

我有实体技能和ManyToMany语言,我需要创建表单,并在此表单中添加技能名称语言,现在我在表单中添加多个' => true,并且有很多语言但是当$ form-> isValid()我有错误

This value is not valid.

表格

invalidValue={array}[2]
   0 = "Java"
   1 = "PHP"

为什么无效我不明白?

这是我的实体技能

class Skill
{
use Timestampable;
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="skill", type="string", length=255)
 */
private $skill;

/**
 * @var \Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages
 *
 * @ORM\ManyToMany(targetEntity="CodeDirectoryProgramLanguages", inversedBy="skills")
 */
protected $language;

/**
 * Constructor
 */
public function __construct()
{
    $this->language = new \Doctrine\Common\Collections\ArrayCollection();
    $this->platforms = new \Doctrine\Common\Collections\ArrayCollection();
    $this->specialities = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add language
 *
 * @param \Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages $language
 *
 * @return Skill
 */
public function addLanguage(\Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages $language)
{
    $this->language[] = $language;

    return $this;
}

/**
 * Remove language
 *
 * @param \Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages $language
 */
public function removeLanguage(\Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages $language)
{
    $this->language->removeElement($language);
}

/**
 * Get language
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getLanguage()
{
    return $this->language;
}

这是我的表格

class SkillType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('mainSkill','checkbox');
            $builder->add('language','entity',
                array(
                    'class'=>'Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages',
                    'multiple'=>true,
                    'property'=>'languages', )
            );
    $builder
    ->add('edit','submit')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Artel\ProfileBundle\Entity\Skill',
        'csrf_protection' => false
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'skill';
}
}

以及我在$ form->提交时的操作($ request);有这个错误

/**
 * Edit skill.
 *
 * @Route("/skills/edit/{id}", name="skill_edit")
 * @Method({"GET", "POST"})
 * @Template()
 * @ParamConverter("entity", class="MyBundle:Skill")
 */
public function editSkillsAction(Request $request, $entity)
{
    $em = $this->getDoctrine()->getManager();
    $form = $this->createForm(new SkillType(), $entity);
    if ($request->isMethod('POST')) {

        $form->bind($request);
        if ($form->isValid()) {

            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('skills_show'));
        }
    }

    return array(
        'debug' => true,
        'form'      => $form->createView(),
    );
}

更新 我尝试添加表格'扩展' => true,一切正常,doctrine在表中创建ManyToMany关系。也许还有另一种方式吗?

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('mainSkill','checkbox');
            $builder->add('language','entity',
                array(
                    'class'=>'Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages',
                    'property'=>'languages',
                    'multiple'=>true,
                    'expanded' => true,
                     )
            );
    $builder
    ->add('edit','submit')
    ;
}

0 个答案:

没有答案