首先:我得到的错误是:传递到选择字段的实体必须被管理
我有这些实体:-用户(属于一个或多个团队)-团队(一名或两名用户)-挑战赛(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()));
}
}
您已经创建了显示ManyToMany
集合的表单;在formbuilder中为这些小部件设置multiple
选项为true
(默认为false,这从根本上与ToMany
关系冲突)。
如果使用表单类型时,两个实体之间的ManyToMany
关系出现Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
错误,可能来自您的实体构造函数:
如果你的表单是"TeamType",试着移除你的"Team"实体的ArrayCollection初始化。
你的Team类变成:
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;
}
}
问题解决了。我不得不在ChallengeType类中添加"allow_add"选项到我的集合。
挑战控制器类也需要一些编辑。在将其传递到表单之前,我向Challenge对象添加了2个团队。