一种形式 - 添加到两个表

发布于 2024-11-15 01:37:34 字数 775 浏览 6 评论 0原文

我如何在 Symfony 1.4 和 Doctrine 中制作一种表单,将数据添加到两个表中? 默认 Symfony 为一个表和模块生成表单。我什么时候可以编辑它并添加自己的字段? http://www.symfony-project.org/jobeet/1_4/Doctrine /zh/03 生成的。 例如,我想添加具有新类别的字段。

# config/doctrine/schema.yml
JobeetCategory:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true, unique: true }

JobeetJob:
  actAs: { Timestampable: ~ }
  columns:
    category_id:  { type: integer, notnull: true }
     (...)
    expires_at:   { type: timestamp, notnull: true }
  relations:
    JobeetCategory: { onDelete: CASCADE, local: category_id, foreign: id, foreignAlias: JobeetJobs } 

此表单仅添加 id JobeetJob。我怎样才能添加到 JobeetCategory 中?

how can i in Symfony 1.4 and Doctrine make one form, that will add data to two tables?
Default Symfony generated form for one table and module. at which point I can edit it and add their own fields?
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03
generated.
i would like for example add field with new category.

# config/doctrine/schema.yml
JobeetCategory:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true, unique: true }

JobeetJob:
  actAs: { Timestampable: ~ }
  columns:
    category_id:  { type: integer, notnull: true }
     (...)
    expires_at:   { type: timestamp, notnull: true }
  relations:
    JobeetCategory: { onDelete: CASCADE, local: category_id, foreign: id, foreignAlias: JobeetJobs } 

this form add only id JobeetJob. how can i add into also JobeetCategory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2024-11-22 01:37:34

您可以将 JobeetCategory 关系嵌入到 JobeetJob 表单中。这将使您能够创建职位和类别。查找 sfForm 类的“embedRelation()̀”方法。

You can embed the JobeetCategory relation in the JobeetJob form. This will enable you to create a job and a category. Look for the `embedRelation()̀ method of the sfForm class.

纸短情长 2024-11-22 01:37:34

您应该覆盖模型中的 save 方法(假设在 JobeetJob 模型中)。并将两个模型保存在事务中。下面我保存到产品和复合产品表。但不要忘记 save 方法应该有 2 个功能:插入更新

public function save(Doctrine_Connection $conn = null){
  try {
      $conn->beginTransaction();

      $isNew = $this->isNew();         
      parent::save($conn); #Save Product

      $modelName = $this->moduleArray[$moduleName];

      $productId = $this->getId();

      if($isNew){ #CREATE new Composite Product and set Product ID and Module Name to it.

          $cp = new CompositeProduct();
          $cp->setRelatedModelID($productId);
          $cp->setRelatedModelName($modelName);
          $cp->setTitle($this->getTitle());
          $cp->save();

      }else{# UPDATE the Composite Product

          $query = Doctrine_Core::getTable('CompositeProduct')
                                ->createQuery('cp')
                                ->where('cp.relatedmodelname = ?',  $modelName)
                                ->andWhere('cp.relatedmodelid = ?', $productId);
          $cp = $query->fetchOne();

          $cp->setTitle($this->getTitle());
          $cp->save();
      }

      $conn->commit();
  }catch(Doctrine_Exception $e){
      $conn->rollback();
  }

}

You should override save method in your model(Lets say in JobeetJob model). And save both models in transaction. Below I save to product and composite product tables. But dont forget that save method should has 2 functionality : insert and update

public function save(Doctrine_Connection $conn = null){
  try {
      $conn->beginTransaction();

      $isNew = $this->isNew();         
      parent::save($conn); #Save Product

      $modelName = $this->moduleArray[$moduleName];

      $productId = $this->getId();

      if($isNew){ #CREATE new Composite Product and set Product ID and Module Name to it.

          $cp = new CompositeProduct();
          $cp->setRelatedModelID($productId);
          $cp->setRelatedModelName($modelName);
          $cp->setTitle($this->getTitle());
          $cp->save();

      }else{# UPDATE the Composite Product

          $query = Doctrine_Core::getTable('CompositeProduct')
                                ->createQuery('cp')
                                ->where('cp.relatedmodelname = ?',  $modelName)
                                ->andWhere('cp.relatedmodelid = ?', $productId);
          $cp = $query->fetchOne();

          $cp->setTitle($this->getTitle());
          $cp->save();
      }

      $conn->commit();
  }catch(Doctrine_Exception $e){
      $conn->rollback();
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文