I have three entities like so:
-Available-Teams(由管理员管理)
-Player-PreConfig(由管理员管理)
-Player-Self](由用户管理(玩家本身))
可供团队:
-
所有可用的团队
玩家PreConfig:
-
这里管理员可以预先选择允许玩家玩的团队 . (First-Filter - Many2Many:Available-Teams < - > Player-PreConfig) - 视图中有很多复选框 .
玩家自:
-
这里玩家应该能够选择他想参加的球队(多个) . 但他不应该列出所有可能的可用球队,但只列出其余球队 .
Classes
/**
* TeamsPlayerBundle\Entity\Teams
*
* @ORM\Table(name="team")
* @ORM\Entity
*/
class Team
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="PreConfig", mappedBy="teams", cascade={"persist", "remove"})
**/
private $configs;
/**
* @ORM\ManyToMany(targetEntity="Player", mappedBy="teams2show", cascade={"persist"})
**/
private $players;
public function __construct()
{
$this->configs = new ArrayCollection();
$this->players = new ArrayCollection();
}
(... setters and getters)
###################################################
/**
* TeamsPlayerBundle\Entity\PreConfig
*
* @ORM\Table(name="preconfig")
* @ORM\Entity
*/
class PreConfig
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Teams", inversedBy="configs", cascade={"persist", "remove"})
* @ORM\JoinTable(name="preconfig_teams)
**/
private $teams;
public function __construct()
{
$this->teams = new ArrayCollection();
}
(... setters and getters)
####################################################
/**
* TeamsPlayerBundle\Entity\Player
*
*
* @ORM\Table(name="player")
* @ORM\Entity
*/
class Player
{
/**
* @var integer $player_id
*
* @ORM\Column(name="player_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $player_id;
/**
* @var string $name
* @Assert\NotBlank
*
* @ORM\Column(name="name", type="string", length=64)
*/
private $name
/**
* @ORM\ManyToMany(targetEntity="Team", inversedBy="player", cascade={"persist"})
* @ORM\JoinTable(name="player_team",
* joinColumns={@ORM\JoinColumn(name="player_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="id", referencedColumnName="id")}
* )
**/
private $teams2show;
public function __construct()
{
$this->teams2show = new ArrayCollection();
}
(... setters and getters)
Right now i have this FormType: I try to solve with Query_Builder as suggested by "Viktor77"
namespace TeamsPlayerBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;
use TeamsPlayerBundle\Entity\Player;
class Teams2ShowType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('teams2show', 'entity', array(
'class' => 'TeamsPlayerBundle\Entity\PreConfig',
'query_builder' => function(EntityRepository $er) use ($cid) {
return $er->createQueryBuilder('c')
->add('orderBy', 'c.name ASC')
->innerJoin('c.teams', 'c2')
->where('c2.id = :configId')
->setParameter('configId', $cid);
},
'expanded' => true,
'multiple' => true,
'property_path' => 'teams2show',
'property' => 'name'
))
;
...
For Your Reference: => My first Form looked like this:
class Teams2ShowType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('teams2show', 'entity', array(
'multiple' => true,
'expanded' => true,
The problem was as follows:
如果我现在渲染表单一切正常,但会呈现一个巨大的复选框列表 . 提出整个实体 .
当然,因为我不知道,如何只根据许多关系可用 - 团队< - > Player-PreConfig填充剩余的实体 .
因为很明显我的实际Teams2ShowType不知道,只剩下剩下的团队应该出现 .
我已经尝试了很多并且阅读了很多(query_builder,模型变换器等等),但我无法正确使用它 .
我的真实例子(在公司中)与许可人和合作伙伴配置有关,但我想在一个更易于理解的场景中提出这个问题 .
我不知道如何实现这一权利的任何最佳实践 .
非常感谢你的帮助,我已经尝试解决这个问题超过3-4天了 .
亲切的问候,
1 回答
query_builder选项是要走的路 . 只需使用Doctrine Query Builder API来获取您需要在表单中呈现的实体,而不是所有实体