Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing value for primary key id Doctrine Symfony2

I'm working on a join between two entities in doctrine on symphony 2.8.2. I keep getting "Missing value for primary key id"

Heres the id annotation for the missing id.

 /**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

Here are my joins:

/**
 * @ORM\OneToOne(targetEntity="FYP\BaseDesignBundle\Entity\SessionDesign", inversedBy="user")
 * @ORM\JoinColumn(name="fcid", referencedColumnName="id")
 */
private $sessionDesign;


/**
 * @ORM\OneToOne(targetEntity="FYP\UserBundle\Entity\User", inversedBy="sessionDesign")
 * @ORM\JoinColumn(name="id", referencedColumnName="fcid")
 */
private $user;
like image 336
Jeremy Avatar asked Feb 23 '16 15:02

Jeremy


1 Answers

It's a mistake coming from the joinColumn name of your association.

Change your mapping to :

/**
 * @ORM\OneToOne(targetEntity="FYP\UserBundle\Entity\User", inversedBy="sessionDesign")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

Note: That is the default configuration, also the line can be removed because it's useless.

EDIT

I was right without pointing the real problem.
You are getting this error because you are trying to use a column that is not a primary key as the referencedColumnName of your joinColumn

The following:

* @ORM\JoinColumn(name="id", referencedColumnName="fcid")

Should be:

* @ORM\JoinColumn(name="user_id", referencedColumnName="id")

From this similar question at the owner's answer (related to the exactly same error):

It is not possible to use join columns pointing to non-primary keys. Doctrine will think these are the primary keys and create lazy-loading proxies with the data, which can lead to unexpected results. Doctrine can for performance reasons not validate the correctness of this settings at runtime but only through the Validate Schema command.

Similar question Is it possible to reference a column other than 'id' for a JoinColumn?

like image 99
chalasr Avatar answered Oct 15 '22 22:10

chalasr