首页 文章

错误:必须管理传递给选择字段的实体

提问于
浏览
1

首先:我得到的错误是:必须管理传递给选择字段的实体

我有这些实体: - 用户(属于一个或多个团队) - 团队(有一个或两个用户) - 挑战(有2个团队)

我想构建一个ChallengeType表单,用户可以为两个团队填写两个用户并创建挑战 . 我想我需要一个嵌入式表格 .

我已经制作了一个TeamType Form类:(我希望从中获得一个选择框,其中列出了所有用户)

<?php

namespace Tennisconnect\DashboardBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class TeamType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('players', 'entity', array(
            'class' => 'TennisconnectUserBundle:User',
            'multiple' => true
        ));
    }

    public function getName()
    {
        return 'team';
    }

    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Tennisconnect\DashboardBundle\Entity\Team');
    }
}

这是ChallengeType表单类:

<?php

namespace Tennisconnect\DashboardBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ChallengeType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('teams', 'collection', array('type' => new TeamType()));
    }

    public function getName()
    {
        return 'challenge';
    }

    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Tennisconnect\DashboardBundle\Entity\Challenge');
    }
}

挑战实体:

namespace Tennisconnect\DashboardBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Tennisconnect\DashboardBundle\Entity\Team;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
* @ORM\Entity
* @ORM\Table(name="challenge")
*/
class Challenge
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Team", mappedBy="teams")
     */
    protected $teams;

    public function __construct()
    {
        $this->teams = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add teams
     *
     * @param Tennisconnect\DashboardBundle\Entity\Team $teams
     */
    public function addTeam(Team $teams)
    {
        $this->teams[] = $teams;
    }

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

团队实体:

namespace Tennisconnect\DashboardBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Tennisconnect\UserBundle\Entity\User;
use Tennisconnect\DashboardBundle\Entity\Challenge;
use Tennisconnect\DashboardBundle\Entity\Match;

/**
* @ORM\Entity
* @ORM\Table(name="team")
*/
class Team
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Tennisconnect\UserBundle\Entity\User",     mappedBy="teams")
     */
    protected $players;

     /**
     * @ORM\ManyToMany(targetEntity="challenge", inversedBy="teams", cascade=     {"persist"})
     */
    protected $challenges;

    /**
     * @ORM\ManyToMany(targetEntity="Match", inversedBy="teams")
     */
    protected $matches;

    public function __construct()
    {
        $this->players = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add players
     *
     * @param Tennisconnect\UserBundle\Entity\User $players
     */
    public function addUser(User $players)
    {
        $this->players[] = $players;
    }

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

    /**
     * Add matches
     *
     * @param Tennisconnect\DashboardBundle\Entity\Match $matches
     */
    public function addMatch(Match $matches)
    {
        $this->matches[] = $matches;
    }

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

    /**
     * Add challenges
     *
     * @param Tennisconnect\DashboardBundle\Entity\challenge $challenges
     */
    public function addchallenge(challenge $challenges)
    {
        $this->challenges[] = $challenges;
    }

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

挑战控制器:

class ChallengeController extends Controller
{
    public function newAction()
    {
        $challenge = new Challenge();
        $form = $this->createForm(new ChallengeType(), $challenge);

        return $this->render('TennisconnectDashboardBundle:Challenge:new.html.twig', array('form' => $form->createView()));
    }
}

3 回答

  • 0

    您已创建显示 ManyToMany 集合的表单;将formbuilder中的 multiple 选项设置为 true (默认为false,从根本上与 ToMany 关系冲突) .

  • 1

    如果您的错误 Entities passed to the choice field must be managed. Maybe persist them in the entity manager? 与2个实体之间存在 ManyToMany 关系,则在使用表单类型时,它可能来自您的实体 constructor

    如果您的表单是“TeamType”,请尝试删除“Team”实体的ArrayCollection初始化 .

    您的团队成员:

    namespace Tennisconnect\DashboardBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Doctrine\Common\Collections\ArrayCollection;
    use Tennisconnect\UserBundle\Entity\User;
    use Tennisconnect\DashboardBundle\Entity\Challenge;
    use Tennisconnect\DashboardBundle\Entity\Match;
    
    /**
    * @ORM\Entity
    * @ORM\Table(name="team")
    */
    class Team
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @ORM\ManyToMany(targetEntity="Tennisconnect\UserBundle\Entity\User",     mappedBy="teams")
         */
        protected $players;
    
         /**
         * @ORM\ManyToMany(targetEntity="challenge", inversedBy="teams", cascade=     {"persist"})
         */
        protected $challenges;
    
        /**
         * @ORM\ManyToMany(targetEntity="Match", inversedBy="teams")
         */
        protected $matches;
    
        public function __construct()
        {
            // REMOVE $this->players = new ArrayCollection();
        }
    
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Add players
         *
         * @param Tennisconnect\UserBundle\Entity\User $players
         */
        public function addUser(User $players)
        {
            $this->players[] = $players;
        }
    
        /**
         * Get players
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getPlayers()
        {
            return $this->players;
        }
    
        /**
         * Add matches
         *
         * @param Tennisconnect\DashboardBundle\Entity\Match $matches
         */
        public function addMatch(Match $matches)
        {
            $this->matches[] = $matches;
        }
    
        /**
         * Get matches
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getMatches()
        {
            return $this->matches;
        }
    
        /**
         * Add challenges
         *
         * @param Tennisconnect\DashboardBundle\Entity\challenge $challenges
         */
        public function addchallenge(challenge $challenges)
        {
            $this->challenges[] = $challenges;
        }
    
        /**
         * Get challenges
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getChallenges()
        {
            return $this->challenges;
        }
    }
    
  • 3

    问题已经解决了 . 我必须在ChallengeType类中为我的集合添加“allow_add”选项 .

    挑战控制器类也需要一些编辑 . 我将2个团队添加到Challenge对象中,然后将其传递给表单 .

相关问题