views:

20

answers:

2

two models Site and Language share a many-to-many relationship (they are bi-directional) How do I add a relationship between them?

Ideally I want to do this : (add an existing language to a new Site)

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

---- code to add language----

$site->save();

Or should I only add the language after calling save() and if so how is that done?

Thanks in advance

A: 

I really cannot offer much better explanation than that:

http://www.doctrine-project.org/projects/orm/1.2/docs/manual/working-with-models/en#many-to-many-relations

Tomasz Struczyński
A: 

Found the problem : The primary key of the association table is not set to "Auto-Increment"

this code works

$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();
rahul