2012-02-29 15 views
1

私はSymfony 1.4で埋め込まれた関係/フォームで遊んでいましたが、解決できない問題がありました。symfony 1.4との埋め込み関係

私は何をしたいです:

  • ユーザーが新しいイベントを作成するときに、私は、ユーザーが既存の都市を選択することができる可能性が
  • 目的地と出発のための関係/フォームを埋め込みますsfWidgetFormJQueryAutocompleterを介して、都市がDBに存在しない場合は、新しい都市が挿入され、出発地/目的地とイベントとの関連付けが行われます。私は単純に新しい街がある場合、それは動作しますが、私は戻って取得するときに、既存の都市(ID、名前、国がcorrecltyある私の町のフォーム

    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が無効であると言って失敗します。 symfonyは新しいオブジェクトを作成しようとするので、私が何をしようとした何

は、idの私のデフォルトのバリデータを修正し、それが検証に合格したが失敗し

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

にベースバリ(sfValdidatorChoice)を変換することですまったく同じ値で彼らはすでに存在しているときに、必要なときに今では都市を作成

public function save(Doctrine_Connection $con = null) { 

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

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

ではなく:

は今、私はそのような何かにCityの保存メソッドをオーバーライドしてみました。

私の新しい問題は、イベント挿入に失敗することです。ここで

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

OK:それは言って新しい例外を投げます長い一日の後に私は解決策を見つけました...しかし、私はそれが最も正しい解決策であると確信していません...

私は自分のフォームで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); 
} 
関連する問題