与symfony 1.4的嵌入式关系

时间:2012-02-29 16:11:39

标签: php forms symfony1 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,姓名和国家/地区已经填充)时,它会失败,说Id无效。

我尝试做的是更正我的id的默认验证器并将基本验证器(sfValdidatorChoice)转换为

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

它通过验证但失败,因为Symfony尝试创建一个具有完全相同值的新对象。

现在我尝试将City的save方法覆盖为类似的东西:

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 个答案:

答案 0 :(得分: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);
}