Doctrine为瞬态记录添加多对多关系

时间:2010-07-13 05:38:50

标签: orm doctrine many-to-many record transient

两种模式网站和语言共享多对多关系(它们是双向的) 如何在它们之间添加关系?

理想情况下,我想这样做:(将现有语言添加到新网站)

$site = new Site();
$site->name = "Google"
$site->url = "www.google.com";

----添加语言的代码----

$site->save();

或者我应该在调用save()之后添加语言,如果是这样,那该怎么办?

提前致谢

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

发现问题:关联表的主键未设置为“自动增量”

此代码有效

$site = new Site();
$site->name = "Google";
$site->url = "www.google.com";
// now add languages
$langIds = array(1, 2,3);
foreach ($langIds as $id) {
    $site->SiteLanguage[]->languageId = $id;
}

// now call save --- this creates a new site along with associations
$site->save();