dstnlhhv791576 2011-12-04 20:39
浏览 33
已采纳

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

First of all: the error I'm getting is: Entities passed to the choice field must be managed

I have these entities: - user (belongs to one or many teams) - team (has one or 2 users) - challenge (has 2 teams)

I'd like to build a ChallengeType form where a user can fill in the two users for the two teams and create the challenge. I think I need an embedded form here.

I've made a TeamType Form class: (I would expect to get a select box from this, where all users are listed)

<?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');
    }
}

This is the ChallengeType form class:

<?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');
    }
}

Challenge entity:

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;
    }
}

Team entity:

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;
    }
}

Challenge controller:

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条回答 默认 最新

  • dsvjmc0907 2011-12-08 08:20
    关注

    The problem is solved. I had to add the "allow_add" option to my collection in the ChallengeType class.

    The challenge controller class needed some editing too. I added 2 teams to the Challenge object before passing it through to the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况