开发者

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].


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜