问题描述
我有 2 个实体:
团队(id、名称、short_name...)
TeamMembers(id、teams_id、player_id)
在管理员中,我有多选字段,并在提交时发送 ids 数组。如何将其保存在相关表中?
这是我在 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?
}
附言另外,我需要处理删除...
提前致谢...
解决方法
您需要加载将它们映射到实体所需的实体。
您最好在此处查看有关关联映射的学说/orm 文档:https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/association-mapping.html
,您只需在加载图像时加载实体。
$entity->setPlayers($this->playerRepository->findById($data['players']));