Skip to content

Instantly share code, notes, and snippets.

@jtallant
Created February 25, 2016 22:22
Show Gist options
  • Save jtallant/0b84abe6f670cede25a3 to your computer and use it in GitHub Desktop.
Save jtallant/0b84abe6f670cede25a3 to your computer and use it in GitHub Desktop.
Doctrine Foreign Key Management
<?php
class Post
{
public function addComment(Comment $comment)
{
$this->comments->add($comment);
$comment->setPost($this);
}
}
$post = new Post;
$post->addComment($comment);
$em->persist($post);
$em->flush();
# Comment now has FK
# Keeping entity relationships in sync is pretty easy.
# An even easier way to do it is to add cascade={"persist"} to the annotation.
# Then you don't have to do $comment->setPost($post);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment