tmdbGateway->getMovieCredits($movie->getTmdbId()); foreach ($creditsContext->cast as $actorModel) { $actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]); if (!$actor instanceof Actor) { $actor = new Actor() ->setPopularity($actorModel->popularity) ->setName($actorModel->name) ->setTmdbId($actorModel->id); $this->em->persist($actor); } $existingRole = $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $movie]); if (0 === $existingRole) { $role = new MovieRole() ->setMovie($movie) ->setActor($actor) ->setCharacter($actorModel->character); $this->em->persist($role); } } } }