2012-02-29 6 views
1

나는 Symfony 1.4와 임베디드 관계/폼을 가지고 놀았지만 해결할 수없는 문제가 있습니다.symfony 1.4와 임베디드 관계

내가하고 싶은 것입니다 :

  • 사용자가 새로운 이벤트를 생성 할 때, 나는 사용자는 기존의 도시를 선택 할 수
  • 목적지와 출발을위한 관계/형태를 포함 sfWidgetFormJQueryAutocompleter를 통해 도시가 DB에 존재하지 않으면 새로운 도시가 삽입되고 출발/목적지와 이벤트 사이의 연관이 만들어집니다.

는 단순히

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에 대한 내 기본 유효성 검사기를 수정하고

$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); 
    } 

가 지금은 필요할 때 도시를 만들 수 있지만 이미있을 때 존재하지 :

은 지금은 그런 일에 도시의 저장 방법을 재정의했습니다.

내 새로운 문제는 이벤트 삽입에 실패한 것입니다.

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

여기

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

좋아 내 schema.yml 파일입니다 (이 경우, city_departure는 새와 목적지 기존 WAS) : 그것은 말하는 새로운 예외를 던져 긴 하루가 지난 후에 나는 해결책을 찾았습니다 ...하지만 가장 정확한 해결책이라고 확신하지 못합니다 ...

저는 제 형식으로 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); 
} 
관련 문제