I have three entities like so:
-Available-Teams (Managed by Admins)
-Player-PreConfig (Managed by Admins)
-Player-Self] (Managed by User(Player itself))
Available-Teams:
--> All available teams
Player-PreConfig:
--> Here the Administrators are able to preselect teams in which a player is allowed to play. (First-Filter - Many2Many: Available-Teams<->Player-PreConfig) - Lots of checkboxes in the view.
Player-Self:
--> Here the Player should be able to select the teams (multiple) he would like to play in. But he should not get listed ALL possible Available-Teams, but only the remaining ones.
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:
If I render the form right now everything works fine, but a huge list of checkboxes gets rendered. The whole entity is presented.
Sure because I have no idea, how to only populate the remaining entities depending on the manytomany relationship Available-Teams<->Player-PreConfig).
Because obviously my actual Teams2ShowType has no idea, that only the remaining teams should show up.
I have already tried a lot and read alot (query_builder, model transformer, etc..), but I could not get it right.
My real example (in company) has todo something with licensors and partner configuration, but i wanted to present this question in a more comprehensible scenario.
I do not know of any best practice how to implement this right.
Thank you so much for your help in advance I already try to solve that issue more than 3-4days.
Kind regards,