whamsicore
whamsicore

Reputation: 8720

Symfony: How to save record with undefined foreign key record

For example, Comment table has foreign key at Author table. How do I create a new author record when I save a new comment? e.g.: Comment: id = 1, author_id = [newly generated id associated with author table], content = "this is a new comment". Author: id = 1, author_name = [newly generated author name].

Upvotes: 2

Views: 537

Answers (1)

Crozin
Crozin

Reputation: 44396

This has nothing to do with Symfony. I assume you're using Doctrine, am I right? Well, all you have to do is to create Comment and Author objects:

$author = new Author();
$author->setName('Crozin');

$comment = new Comment();
$comment->setAuthor($author);
$comment->setContent('This is my first comment!');

$comment->save();

Doctrine should recognize that you're using two brand new objects that aren't persisted in database and thus both object will be inserted.

Upvotes: 4

Related Questions