2012-02-29 55 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); 
}