-1

I have 2 entities:

Teams (id, name, short_name...)
TeamMembers (id, teams_id, player_id)

In admin, I have multiselect field, and on submit it sends ids array. How can I save it in related table?

Here is a part of code that I have in TeamsController

/**
 * @param array<string, mixed> $data
 */
protected function mapDataToEntity(array $data, Teams $entity): void
{
    $image = null;

    if ($imageId = ($data['logo']['id'] ?? null))
    {
        $image = $this->mediaRepository->findMediaById($imageId);
    }
    $entity->setLogo($image);
    $entity->setName($data['name']);
    $entity->setCountry($data['country']);
    $entity->setGender($data['gender']);
    $entity->setShortName($data['shortName']);
    $entity->setPlayers($data['players']); // <-- how do I handle THIS?
}

P.S. Also, I need to handle deletion...

Thanks in advance...

2 Answers2

1

You need to load the entities you need to map them to your entity.

At the best you have here a look at the doctrine/orm documentation about association mapping: https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/association-mapping.html

Alexander Schranz
  • 2,154
  • 2
  • 25
  • 42
0

You just have to load the entities as you load the image. $entity->setPlayers($this->playerRepository->findById($data['players']));

jona303
  • 1,358
  • 1
  • 9
  • 27