Не получается создать отношение OneToOne в Doctrine
Вот мой пример:
class User {
...
/**
* @ORM\Column(type="string", length=100)
* @ORM\OneToOne(targetEntity="UserData", mappedBy="user_email")
*/
private $email;
...
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string $email
*
* @return User
*/
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
}
class UserData {
...
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="email")
* @ORM\JoinColumn(name="id_user", referencedColumnName="id", onDelete="CASCADE")
*/
private $user_email;
...
/**
* @return mixed
*/
public function getEmail()
{
return $this->user_email;
}
}
UserDataRepository:
public function findUserById(int $userId)
{
$entityManager = $this->getEntityManager();
$query = $entityManager->createQuery(
'SELECT ud, u.email as user_email, u.login as user_login
FROM App\Entity\UserData ud
LEFT JOIN App\Entity\User u WITH ud.id_user = u.id
WHERE ud.id_user = :userId'
)->setParameter('userId', $userId);
return $query->getOneOrNullResult();
}
Но результат говорит
Notice: Undefined index: email