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

时间:2011-12-04 20:39:47

标签: php forms symfony

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

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

我想构建一个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 :(得分:3)

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

答案 1 :(得分:1)

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

如果您的表单是&#34; TeamType&#34;,请尝试删除您的&#34; Team&#34;的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;
    }
}

答案 2 :(得分:0)

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

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