I use doctrine, User and City entities are OneToOne to each other, When I call user entity, first I want to check if that entity has a city or not, if has it, update it, if doesn’t have it, create a city entity for that user.
I have this hasser method in my User entity:
public function hasCity() {
return $this->city instanceof City;
}
Then in my script I have:
$user = $em->find(User.....);
if ($user->hasCity()) {
$city = $user->getCity();
$zip = $city->getZip();
} else {
$city = new City;
$city->setUser($user);
}
But I still get Entity City not found error. I did a print debug and noticed it never goes to else clause even if it doesn’t have city so I get Entity City not found error. What wrong I did in my script above?
This one works niether:
$user = $em->find(User...);
$city = $user->getCity();
if ($city !== null) {
It always assumes User has a City and never goes to else clause. There is no such problem for other mappings than OneToOne.
I guess this should because that both entities are mapped by identifiers of each other (as they are one to one) rather than the owning side having a FK? Should be that the issue?