m4tm4t m4tm4t - 1 month ago 23
PHP Question

doctrine 2 one-to-one not working as expected

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)

Answer

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.