simPod
simPod

Reputation: 13496

Doctrine – how to set up one-to-one relationship between two entities

I have two tables: Users and Contacts

Users

  • id
  • username

Contacts

  • id
  • user_id
  • email (I have simplified the structure)

Now, how to setup doctrine entities properly?

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseEntity {

    /**
     * @Id
     * @GeneratedValue
     * @Column(type="bigint")
     */
     protected $id;

    /**
     * @ORM\Column(type="string", unique=true)
     */
    protected $username;

    /**
     * @ORM\OneToOne(targetEntity="Contact")
     * @ORM\JoinColumn(name="id", referencedColumnName="user_id", onDelete="CASCADE")
     **/
    protected $contact;
}

Contact entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="contacts")
 */
class Contact extends BaseEntity {

    /**
     * @Id
     * @GeneratedValue
     * @Column(type="bigint")
     */
     protected $id;

    /**
     * @var User
     * @ORM\OneToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @ORM\Column(type="string")
     */
    protected $email;
}

The thing is that I'm not sure whether the entity relationship is set properly.

  1. I don't know how to set if User is deleted then delete Contact but not the other way.
  2. If I create $user = new User() and then $contact = new Contact() how to join them? $user->contact = $contact? Will it after persist() and flush() fill user_id correctly and insert data into both tables?
  3. I'm getting error A new entity was found through the relationship '...\User#contact' that was not configured to cascade persist operations for entity: ...\Contact@0000000015f3aa5e000000012cd799f5. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement '...\Contact#__toString()' to get a clue. I'm stuck on this one which I think is related to my problem #1 and therefore I can't test #2.

I have been browsing docs for hours now but I got stuck and I didn't find any real example that would guide me... Can anyone help me by showing me proper configuration of these entities' relationship?

Upvotes: 3

Views: 2867

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32402

In order to cascade a user deletion so its contact also gets deleted, in the Contact entity add onDelete="CASCADE" to the JoinColumn annotation of the $user property (this will only delete a contact if its user is deleted)

/**
 * @var User
 * @ORM\OneToOne(targetEntity="User")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $user;

If you persist the contact before adding it to the user you should not get any error. If you want to add a new contact by directly assigning the new un-persisted contact to a user, then you'll need to add cascade={"persist", "remove"} to your User entity

/**
 * @ORM\OneToOne(targetEntity="Contact",cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="id", referencedColumnName="user_id")
 **/
protected $contact;

Upvotes: 6

Related Questions