简体   繁体   中英

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;

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM