I have a AR called User
, which has among other entities one called City
.
The way I assign a City
to a User
is:
$user->addCity($city);
The problem comes when a id
is given at some Controller endpoint instead of a City
entity, I can't relate them if it's not through an instance and I can't retrieve the entity intance without a repository.
I could retrieve the entity with a CityRepository
and then add it to the User
but I though I should only use a repository for ARs.
For the moment, I'm getting the reference through Symfony's EntityManager:
public function __construct(UserRepository $users)
{
$this->users = $users;
}
public function execute(Passenger $user, int $cityId, string $companyName = '')
{
$city = $this->users->getCityReferenceById($cityId);
$user->addCity($city);
....
}
while on UserRepository:
public function getCityReferenceById(int $cityId): City
{
return $this->em->getReference(City::class, $cityId);
}