How to set up a manifold in Symfony2

I have three objects, ChannelEntity → MatchChannelEntity <- MatchEntity, MatchChannelEntity saves many of the many relationships between the other two tables, I want the form to display all the channels using the checkboxes, and if there is one channel in one of them, the checkbox of this channel is selected , How can i do this?

Here is the form type code:

class MatchhType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('channels', 'entity', array('label' => 'Channels', 'class' => 'Mikay\MikiBundle\Entity\Channel', 'multiple' => true, 'expanded' => true, 'query_builder' => function ($repository) { return $repository->createQueryBuilder('c')->orderBy('c.name', 'ASC'); },)) 

MatchChannel Type:

 class MatchChannel { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var integer $match_id * @ORM\ManyToOne(targetEntity="Matchh", inversedBy="channels") * @ORM\JoinColumn(name="match_id", referencedColumnName="id", nullable="true") */ private $match; /** * @var integer $channel_id * * @ORM\ManyToOne(targetEntity="Channel", inversedBy="mathces") * @ORM\JoinColumn(name="channel_id", referencedColumnName="id", nullable="true") */ private $channel; 

I will use an example to explain, say, I have three channels: channel A, channel B and channel C, and one match: match M, match M has one channel A, this relationship is stored in the match_channel table, I want the form correspondence showed all channels, and channel A is checked, because it belongs to match M, others remain unchecked

+6
source share
2 answers

OK, I will close this question. This is because I improperly established the relationship between many of the two tables, and the correct way is as follows: I cut the code a bit:

Many for many: one match has many channels, and one channel has many matches.

Match:

 class Match { /** * @ORM\ManyToMany(targetEntity="Channel", inversedBy="matches") * @ORM\JoinTable(name="match_channels") */ private $channels; 

A source:

 class Channel { /** * @ORM\ManyToMany(targetEntity="Match", mappedBy="channels") */ private $matches; 

Doctrine will automatically create a cross-reference table for you called MatchChannels. Pay attention to JoinTable annotation, this is very important.

And when you are done, you can easily create a diverse form, just like you create other types of forms / fields.

+6
source

I finally found a workaround for this. You can see the source code http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html

+1
source

Source: https://habr.com/ru/post/909927/


All Articles