Answer the question
In order to leave comments, you need to log in
Error regarding OneToOne?
Can't create OneToOne relation in Doctrine
Here is my example:
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;
}
}
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
Answer the question
In order to leave comments, you need to log in
Of course it doesn't work. To begin with, it is worth deciding what $email is - a string or a link to UserData.
Then think about what should be in $user_email.
And read the documentation will never be superfluous.
ps If it's quite simple - OneToOne connects two entities through fields, not the fields themselves.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question