学说2一对一不按预期工作

I'm trying to do bi-directional one-to-one with doctrine.

User model

<?php
/**
 * @Entity
 * @Table(name="User")
 */
class User {
  /**
   * @Id
   * @Column(type="integer")
   * @GeneratedValue
   */
  protected $id;

  /**
   * @OneToOne(targetEntity="Cart",mappedBy="user",cascade={"persist"})
   */
  public $cart;

}

Cart model

<?php
/**
 * @Entity
 * @Table(name="Cart")
 */
class Cart {
  /**
   * @Id
   * @Column(type="integer")
   * @GeneratedValue
   */
  protected $id;

  /**
   * @OneToOne(targetEntity="User",inversedBy="cart")
   * @JoinColumn(name="user_id", referencedColumnName="id")
   */
  public $user;
}

And I want to set the cart like that

<?php
$user->cart = new Cart;
$entityManager->flush();

The cart has been created, but the user_id is not set. (NULL)

What's wrong ?

(I've not created getters/setters for test)

So I did some extended testing to see what could be done.

Your relationship code appears correct. I'd personally drop the join column annotation, as it defaults to that anyway.

You DO need getters/setters inside each of the entities such as:

// In User entity
public function setCart(Cart $cart) {
    $this->cart = $cart;
}

// In Cart entity
public function setUser(User $user) {
    $this->user = $user;
}

You also need to do something like:

$user = new User();
$em->persist($user);

$cart = new Cart();
$cart->setUser($user);
$em->persist($cart);

$user->setCart($cart);

$em->flush();

Using this snippet I was able to save two relationships to the DB and load them again. The documentation specifically states that new owners with cascade do not cascade.

"Doctrine 2 does not cascade the persist operation to all nested entities that are new as well."

In my example, I've specifically set and persisted both the new items. Existing parents with new cascading children don't have this problem.

Hopefully you can use this information to figure out exactly what needs to change.

To simplify:

You DO need getters/setters inside each of the entities such as:

// In User entity
public function setCart(Cart $cart) {
    $this->cart = $cart;
}

// In Cart entity
public function setUser(User $user) {
    $user->setCart($this);
    $this->user = $user;
}

You also need to do something like:

$user = new User();
$em->persist($user);

$cart = new Cart();
$cart->setUser($user);
$em->persist($cart);

$em->flush();

This process "$user->setCart($cart);" is deported to setter of User in cart;

It's a pretty old question but I faced something similar and found another alternative to kinghfb answer. You can also do the following on your inverse side's entity (User in case of this question).

/**
 * User's Constructor
 */
public function __construct()
{
    $this->cart= new Cart();

    $this->cart->setUser($this);
}

Note that you will need getters and setters as suggested in accepted answer but after doing above, you do not need to create and persist the Cart entity every time you are saving User.

Caution: This solution is for the case where a user will always have a cart.

Hope this helps someone facing similar issues. Thanks!