我如何在Symfony 1.4和Doctrine中创建一个表单,将数据添加到两个表中? 默认Symfony为一个表和模块生成表单。那时我可以编辑它并添加自己的字段? http://www.symfony-project.org/jobeet/1_4/Doctrine/en/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?
答案 0 :(得分:1)
您可以在JobeetJob表单中嵌入JobeetCategory关系。这将使您能够创建作业和类别。查找sfForm类的`embedRelation()方法。
答案 1 :(得分:1)
您应该在模型中覆盖保存方法(让我们在JobeetJob模型中说)。并在交易中保存两个模型。下面我保存到产品和复合产品表。但不要忘记保存方法应该有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();
}
}