1

My relation between 2 entities is a OneToOne relation.

1) User Entity:

<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\UserRepository") */ class User { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @var $userContact * * @ORM\OneToOne(targetEntity="UserContact" , inversedBy="User") * @ORM\JoinColumn(name="id", referencedColumnName="user_id", nullable=false) **/ private $userContact; /** * @ORM\Column(type="string", length=255) */ private $last_name; public function getId() { return $this->id; } public function getLastName(): ?string { return $this->last_name; } public function setLastName(string $last_name): self { $this->last_name = $last_name; return $this; } } 
  1. UserContact Entity:

class UserContact { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id() * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(type="integer") */ private $user_id; /** * @ORM\OneToOne(targetEntity="User", inversedBy="UserContact") */ private $user; /** * @ORM\Column(type="string", length=255) */ private $mobile_number; public function getId() { return $this->id; } public function getUserId(): ?int { return $this->user_id; } public function setUserId(int $user_id): self { $this->user_id = $user_id; return $this; } public function getMobileNumber(): ?string { return $this->mobile_number; } public function setMobileNumber(string $mobile_number): self { $this->mobile_number = $mobile_number; return $this; } } 

My User Repository looks like this:

class UserRepository extends ServiceEntityRepository { public function index() { return $this->createQueryBuilder('u') ->innerJoin('u.userContact', 'uc') ->getQuery() ->execute(); } } 

And the controller looks like this:

 public function index() { $users = $em->getRepository('App:User')->index(); } 

It's throwing an error like this one:

Missing value for primary key id on App\Entity\UserContact

6
  • post your complete User and UserContact entities Commented Apr 9, 2018 at 7:23
  • question has edited. Please check Commented Apr 9, 2018 at 7:42
  • I'm not sure, but I believe that OneToOne expects that column set in referencedColumnName (user_id in your case) is a Primary Key. Try move (and/or add) @ORM\Id() in UserContact to user_id Commented Apr 9, 2018 at 7:52
  • You mean to say add this @ORM\Id() on user_id column. ? user_id is ForeignKey of users table. It will not work because @ORM\Id() is using on multiple columns 1 for its primary key and another for user_id. Commented Apr 9, 2018 at 8:03
  • User entities id is primary and user_id on userContact entities is FK of users table. Commented Apr 9, 2018 at 8:04

1 Answer 1

2

In your association, the owning side is User and the inverse side is UserContact. You must use mappedBy for the inverse side instead inversedBy. So your annotation for the $user attribute in User entity must be this:

/** * @ORM\OneToOne(targetEntity="User", mappedBy="UserContact") */ private $user;

https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html

Sign up to request clarification or add additional context in comments.

2 Comments

what do you mean by owning side ?
Owning side in a doctrine relationship is the side that has the foreign key. Please see the link i provided in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.