2012-02-29 44 views
1

我曾經與Symfony 1.4一起玩過嵌入式關係/表單,但是我遇到了一個我無法解決的問題。與symfony 1.4的嵌入關係

我想要做的是:

  • 當用戶創建一個新的事件,我嵌入的目的地和出發
  • 用戶可以能夠選擇現有城市的關係/表格通過sfWidgetFormJQueryAutocompleter,如果城市不存在於數據庫中,則插入新城市,並且出發/目的地與事件之間的關聯被建立。

我想簡單地嵌入這樣的關係,在我的城市形式

public function configure() { 
    parent::configure(); 
    unset($this['city_departure_id']); 
    unset($this['city_destination_id']); 

    $this->embedRelation('Departure', new MyCityForm($this->getObject()->getDeparture())); 
    $this->embedRelation('Destination', new MyCityForm($this->getObject()->getDestination())); 
    ... 
} 

如果有一個新的城市,它的工作原理,但是當我回來的現有城市(ID,姓名和國家都correclty填充),它通過說Id無效失敗。

我試圖做的是糾正我的默認驗證的ID和基本驗證(sfValdidatorChoice)轉換成

$this->setValidator('id', new sfValidatorPass(array('required' => false)));

它通過驗證,但失敗,因爲Symfony的嘗試創建一個新的對象具有完全相同的價值。

現在我試圖來覆蓋城市的保存方法爲這樣的事情:

public function save(Doctrine_Connection $con = null) { 

     if ($this->isNew() && $this->getId() != "") { 

      return $this; 
     } 
     return parent::save($con); 
    } 

現在需要時創建城市,但不是當他們已經存在。

我的新問題是事件插入失敗。它拋出一個新的異常說:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'city_destination_id' cannot be null 

(在這種情況下,city_departure是一個新的,目的現有的)

這裏是我的schema.yml

Event: 
    connection: doctrine 
    tableName: sortie 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    description: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    start_date: 
     type: date(25) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    city_departure_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    city_destination_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    relations: 
    Departure: 
     local: city_departure__id 
     foreign: id 
     class: City 
     type: one 
    Destination: 
     class: City 
     local: city_destination_id 
     foreign: id 
     type: one 
City: 
    connection: doctrine 
    tableName: localite 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    name: 
     type: string(100) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    country: 
     type: string(100) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    relations: 
    Departures: 
     local: id 
     foreign: localite_depart_id 
     type: many 
    Destinations: 
     class: Sortie 
     local: id 
     foreign: localite_destination_id 
     type: many 

回答

1

好經過漫長的一天,我找到了解決方案...但我不確定這是最正確的解決方案...

我重寫了我的表單中的doSave方法。在updateObject調用之後,我檢查id是否存在於值中。它似乎工作...

protected function doSave($con = null) { 
    if (null === $con) 
    { 
     $con = $this->getConnection(); 
    } 

    $this->updateObject(); 

    $v = $this->getValues(); 
    if (isset($v['Departure']['id'])) 
     $this->getObject()->setCityDepartureId($v['Departure']['id']); 
    if (isset($v['Destination']['id'])) 
     $this->getObject()->setCityDestinationId($v['Destination']['id']); 

    $this->getObject()->save($con); 

    // embedded forms 
    $this->saveEmbeddedForms($con); 
}