【问题标题】:Symfony 1.4 forms doesn't save foreign key with embedRelationSymfony 1.4 表单不使用 embedRelation 保存外键
【发布时间】:2012-01-12 15:08:23
【问题描述】:

情况如下:

我有一个模型 A 和模型 B。模型 A 有一个模型 B 的外键 (modelb_id)。

两种模型都有自己的形式。我将模型 B 的表单嵌入到表单 A 中。

class ModelAForm extends ....{

  public function configure(){
    unset($this['modelb_id']);
    $this->embedRelation('ModelB');
  }
}

问题:

当我保存表单时,它确实保存了 ModelB,但它并没有改变 ModelA 中外键的值(在模型 A 中,modelb_id 仍然为空)。

有人知道吗?

【问题讨论】:

    标签: php symfony-1.4 doctrine-1.2


    【解决方案1】:

    我一直遇到embedRelation() 的问题,直到我找到了一个名为ahDoctrineEasyEmbeddedRelationsPlugin 的插件,它解决了我的所有表单嵌入问题。

    我相信最好使用这个插件,而不是一遍又一遍地调试你的嵌入。 :)

    【讨论】:

      【解决方案2】:

      如果您想在保存一个表单时更新相关对象。您可以覆盖 BasesfForm::doUpdateObject 方法。

      在您的 Form.class.php 中,添加如下内容:

       /* If you want to add some logic before updating or update other associated
        * objects, this is the method to override.
        *
        * @param array $values An array of values
        */
        public function doUpdateObject($values) {
          // Handle the normal stuff that this method does
            $this->getObject()->fromArray($values, BasePeer::TYPE_FIELDNAME);
            $obj = $this->getObject();
      
          // and, get the needed value
            $val = $obj->getDesiredPropertyValue();
      
          // use the value to update the related thing
            foreach ($obj->getRelatedObjects() as $related)
                  {
                    $related->setColumnNamedThing($val);
                  }
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多