ypereirareis.github.io icon indicating copy to clipboard operation
ypereirareis.github.io copied to clipboard

Symfony form entity manytomany multiple false

Open ypereirareis opened this issue 10 years ago • 0 comments

    public function setSingleTeam(Team $team = null)
    {
        if (!$team) {
            return;
        }
        $this->teams->add($team);
    }

    public function getSingleTeam()
    {
        if ($this->teams->count() == 0) {
            return null;
        }
        return $this->teams->first();
    }
->add('singleTeam', 'entity', [
                'class' => 'CommonBundle\Entity\Team',
                'by_reference' => true,
                'required' => false,
                'multiple' => false,
            ])
        $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event){
            $form = $event->getForm();
            $data = $form->getData();
            $teams = $data->getTeams();
            $formTeam = $form->get('singleTeam')->getData();
            foreach ($teams as $team) {
                if ($formTeam->getId() != $team->getId()) {
                    $data->getTeams()->removeElement($team);
                }
                $team->getPlayers()->removeElement($data);
            }
        });

ypereirareis avatar Apr 15 '16 08:04 ypereirareis