Symfony ManyToMany Form

I have the essence of skill and language, specialty, the essence of the platform with the ManyToMany relationship, and when I create the skill, I select the language, specialty, platforms and form, and the normal ones are stored and hidden in the database. But when I create a language, specialty or platform, I have a mistake. This value is also invalid on the debug panel in the form. Why I do not understand, please help:

Message Origin  Cause
This value is not valid.    skills  Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[skills] = [0 => AngularJS, 1 => API]

Caused by:

Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "skills": Could not find all matching choices for the given values

Caused by:

Symfony\Component\Form\Exception\TransformationFailedException
Could not find all matching choices for the given values

In objects I delete

/**
 * 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();
}

as well as language, specialty, platforms are also deleted

objects:

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

/**
 * @var \MyBundle\Entity\CodeDirectoryProgramLanguages
 *
 * @ORM\ManyToMany(targetEntity="CodeDirectoryProgramLanguages", inversedBy="skills", cascade={"persist"})
 */
protected $language;

/**
 * @var \MyBundle\Entity\CodeDirectoryPlatforms
 *
 * @ORM\ManyToMany(targetEntity="CodeDirectoryPlatforms", inversedBy="skills", cascade={"persist"})
 */
protected $platforms;

/**
 * @var \MyBundle\Entity\CodeDirectorySpecialities
 *
 * @ORM\ManyToMany(targetEntity="CodeDirectorySpecialities", inversedBy="skills", cascade={"persist"})
 */
protected $specialities;

and example:

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

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

/**
 * @ORM\ManyToMany(targetEntity="MyBundle\Entity\Skill", mappedBy="specialities", cascade={"persist"})
 */
protected $skills;

and add to all objects

    public function __toString()
{
    return $this->string_filed_entity;
}

this is my form

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('specialities');

            $builder->add('skills','entity',
                array(
                    'class'=>'MyBundle\Entity\Skill',
                    'property'=>'skill',
                    'multiple'=>true,
                    'expanded' => true,
                )
            );
}

and action

        $entity = new CodeDirectorySpecialities();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

            $skills = $entity->getSkills()->getValues();
        if(!empty($skills)){
            foreach($skills as $skill){
                $skill->addSpeciality($entity);
                $em->persist($skill);
            }
        }

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

        return $this->redirect($this->generateUrl('specialities_show', array('id' => $entity->getId())));
    }
+4
source share
1 answer

This is because the html5 validator blocks you in your forms of specialties, platforms and languages.

, , :

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('specialities')
        ->add('skills', , 'entity', array(
            'class' => 'AppBundle\Entity\Skill',
            'property' => 'skill',
            'multiple' => true,
            'expanded' => true
          ))
    ;
}

2- .

, :

$entity = new CodeDirectorySpecialities();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);

if ($form->isValid()) {
    $em = $this->getDoctrine()->getManager();

    $entity = $form->getData();
    $em->persist($entity);
    $em->flush();

    return $this->redirect($this->generateUrl('specialities_show', array('id' => $entity->getId())));
}

- " " .

0

Source: https://habr.com/ru/post/1615531/


All Articles