2011-01-28 65 views
1

我有3代表的许多模型1到许多如以下保存多到许多形式手动

(缩短的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) } 

保存在生成​​的自动这些事表格很好。然而,在特殊情况下,我需要在PeerEngagementForm内单独保存nhi和种族。

为了节省健保我已经工作:

function doSave($con=null){ 

    ... 

    if(isset($this->values['nhi'])){ 
    $this->getObject()->getPeer()->setNhi($this->values['nhi']); 
    $this->getObject()->getPeer()->save(); 
    } 

    ... 

,但同样的技术不起作用与

if(isset($this->values['ethnicity_list'])){ 

    $this->getObject()->getPeer()->setEthnicity($this->values['ethnicity_list']); 
    $this->getObject()->getPeer()->save(); 
} 

该错误消息我得到的是,它需要一个Doctrine_Collection。

如何正确创建此集合,或者如何从顶部表单或操作中保存多对多关系?

回答

1

你有没有试图把$ this-> values ['ethnicity_list']放入一个集合中?

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

} 

应该工作,我这样做,我认为。