我是一位symfony新手,我正在嘗試創建一個系統來跟蹤對製造規範的修訂。我正在使用symfony 1.4.9和Doctrine。Symfony使用外鍵保存嵌入式表格
我在規格和修訂之間有一對多的關係。在創建新規範時,還必須創建一個初始「修訂版」,其中包含大部分相關數據。換句話說,我需要能夠同時創建一個規範和一個連接版本。
我已經爲規格和修訂創建了表單。我能夠單獨使用這些表單,並且它們按預期運行。表單在嵌入時正確呈現,並按預期進行驗證。爲現有規範創建一個新的修訂版可以很好地工作。當我嘗試創建一個新規範及其所需修訂時,我的問題就出現了。
修訂版的外鍵是規範的主鍵。在將規格記錄添加到數據庫之前,該主鍵不存在。由於修訂版的外鍵字段不能爲空,因此嵌入的表單保存會失敗並顯示MySQL錯誤。
這是最好的方法是什麼?我的想法是更改模式以允許外鍵爲空。我將能夠保存表單,然後根據表單的值進行查詢以確定新的規範的主鍵被設置爲什麼,那麼我應該能夠將相關修訂的外鍵設置爲該值。這看起來很可笑,但會導致兩個額外的查詢,而不是真正需要的查詢。我可以想到的簡單方法是停止嘗試使用嵌入式表單功能,並切換到兩步完成此操作(創建並保存規範,然後創建並保存相關修訂) - 但是,我的客戶端不需要這個,所以它不是一個選項。
我真的很希望我在框架中丟失了一些東西,並且有一個更簡單的方法。有任何想法嗎?
編輯:
這些表是一個較大系統的兩個部分,所以該模式是複雜的。每個刪除和重命名爲適當的不相關的部分的簡化版本:
Specifications:
connection: doctrine
tableName: specifications
columns:
specification_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: true
specification_name:
type: string(10)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
relations:
Revisions:
local: specification_id
foreign: specification_id
type: many
Revisions:
connection: doctrine
tableName: revisions
columns:
revision_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: true
specification_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
revision_is_current:
type: integer(1)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
revision_effective_date:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
revision_name:
type: string(20)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
revision_deactivated_date:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
revision_filename:
type: string(40)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
relations:
Specifications:
local: specification_id
foreign: specification_id
type: one
RevisionsForm在SpecificationsForm.class.php通過這個命令嵌入到SpecificationsForm:
$newRevision = new RevisionsForm();
$this->embedForm('Revision', $newRevision;
我可以很容易地使整個事情在specification_id已經存在的情況下工作,但我沒有看到一種方法來執行,直到父表單的規範窗體調用$ form-> save()。我覺得我錯過了一些非常明顯的東西,但是symfony文檔在嵌入式表單上非常稀疏。
如果沒有其他解決方案出現,Versionable看起來會完成這項工作(雖然有很多重複的數據)。