Сохранить многие-ко-многим в форме вручную - PullRequest
1 голос
/ 28 января 2011

У меня есть модель с 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) }

Сохранение этих данных в автоматически сгенерированных формах - это хорошо.Однако в особом случае мне нужно сохранить nhi и этническую принадлежность отдельно от 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 Ответ

1 голос
/ 20 февраля 2011

Вы пытались поместить $ this-> values ​​['ethity_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();

}

Должно работать, я делаю это так же, как я думаю.

...