我有这个 schema.yml:
SdrivingMaquina:
connection: doctrine
tableName: sdriving_maquina
actAs: [Timestampable]
columns:
idmaquina: { type: integer(8), fixed: false, unsigned: false, primary: true, autoincrement: true }
idempresa: { type: integer(4), fixed: false, unsigned: true, primary: true, autoincrement: false }
patente: { type: string(12), fixed: false, unsigned: false, primary: false, notnull: true, autoincrement: false }
relations:
Empresa: { local: idempresa, class: SdrivingEmpresa, type: one, foreignType: one, foreignAlias: MaquinaEmpresa, onDelete: CASCADE, onUpdate: CASCADE }
SdrivingMaquinaEmisor:
connection: doctrine
tableName: sdriving_maquina_emisor
actAs: [Timestampable]
columns:
idmaquinaemisor: { type: integer(8), fixed: false, unsigned: false, primary: true, autoincrement: true }
idmaquina: { type: integer(8), fixed: false, unsigned: false, primary: true, autoincrement: false }
idemisor: { type: integer(8), fixed: false, unsigned: false, primary: true, autoincrement: false }
relations:
SdrivingEmisor: { onDelete: CASCADE, local: idemisor, foreign: idemisor, type: one }
SdrivingMaquina: { onDelete: CASCADE, local: idmaquina, foreign: idmaquina, type: one }
这是configure()
关联表单的方法:
public function configure() {
$this->current_user = sfContext::getInstance()->getUser()->getGuardUser();
unset($this['updated_at'], $this['created_at']);
$this->widgetSchema['idempresa'] = new sfWidgetFormInputHidden();
$id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();
$this->setDefault('idempresa', $id_empresa);
$this->widgetSchema['no_emisor'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingEmisor', 'add_empty' => 'Seleccione un Emisor', 'table_method' => 'fillChoice'));
$this->validatorSchema['idempresa'] = new sfValidatorPass();
$this->validatorSchema['no_emisor'] = new sfValidatorPass();
}
我正在尝试保存主要maquina
然后保存关系的数据,SdrivingMaquinaEmisor()
但不知道也没有找到(经过一些谷歌和 Stackoverflow 研究)获取SdrivingMaquina
对象最新 ID 的正确方法。
这就是我在我的save()
方法中所做的(以相同的形式):
public function save($con = null) {
$new_machine = parent::save($con);
$relation = new SdrivingMaquinaEmisor();
$relation->setIdmaquina($new_machine->getIdmaquina());
$relation->setIdemisor($this->values['no_emisor']);
$relation->save();
return $new_machine;
}
但是setIdmaquina()
永远不会得到一个值,所以它会破坏我的应用程序并导致一个 CONSTRAINT 错误,任何可以指出我正确的方向吗?