Reputation: 2539
I have a model with 3 tables Many to 1 to many as below
(shortened schema.yml)
PeerEngagement:
columns:
id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true }
peer_id: { type: integer(4), notnull: true }
relations:
Peer: { local: peer_id, class: Person }
Person:
columns:
id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true }
nhi: { type: string(7) }
name: { type: string(100), notnull: true }
relations:
Ethnicity: { class: Ethnicity, refClass: PersonEthnicity, local: person_id, foreign: ethnicity_id }
PersonEthnicity:
columns:
id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true }
person_id: { type: integer(4), notnull: true }
ethnicity_id: { type: integer(4), notnull: true }
relations:
Person:
class: Person
local: person_id
onDelete: CASCADE
Ethnicity:
class: Ethnicity
local: ethnicity_id
onDelete: RESTRICT
Ethnicity:
columns:
id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true }
name: { type: string(50) }
Saving these things in auto-generated forms is fine. However in a special case I need to save nhi and ethnicity separately from within PeerEngagementForm.
For saving nhi i have working:
function doSave($con=null){
...
if(isset($this->values['nhi'])){
$this->getObject()->getPeer()->setNhi($this->values['nhi']);
$this->getObject()->getPeer()->save();
}
...
but the same technique doesn't work with
if(isset($this->values['ethnicity_list'])){
$this->getObject()->getPeer()->setEthnicity($this->values['ethnicity_list']);
$this->getObject()->getPeer()->save();
}
The error message I get is that it expects a Doctrine_Collection.
How do I create this collection correctly, or how can I save the many to many relationship from within the top form or action?
Upvotes: 1
Views: 799
Reputation: 294
Have you tried to put $this->values['ethnicity_list'] in a Collection ?
if(isset($this->values['ethnicity_list'])){
$ethnicityLists = new Doctrine_Collection('Ethnicity');
foreach($this->values['ethnicity_list'] as $ethnicityList){
$ethnicityLists->add($ethnicityList);
}
$peer = $this->getObject()->getPeer();
$peer->setEthnicity($ethnicityLists);
$peer->save();
}
Should work, I do it the same way I think.
Upvotes: 1