2011-12-29 58 views
1

我有以下型號:嵌入表單不保存正確

WebPromocion: 
    connection: doctrine 
    tableName: WebPromocion 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: true 
     autoincrement: true 
    nombre: 
     type: string(100) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    foto: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: true 
     autoincrement: false 
    flyer: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: true 
     autoincrement: false 
    desde: 
     type: timestamp(25) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    hasta: 
     type: timestamp(25) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    descripcion: 
     type: string() 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    status: 
     type: string(1) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    relations: 
    WebFoto: 
     local: foto 
     foreign: id 
     type: one 
    WebFoto_2: 
     class: WebFoto 
     local: flyer 
     foreign: id 
     type: one 
    WebPromocion_Producto: 
     local: id 
     foreign: promocion 
     type: many 

WebFoto: 
    connection: doctrine 
    tableName: WebFoto 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: true 
     autoincrement: true 
    ruta: 
     type: string(500) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    archivo: 
     type: string(150) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    nombre: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    alt: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '' 
     notnull: true 
     autoincrement: false 
    width: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: true 
     autoincrement: false 
    height: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: true 
     autoincrement: false 
    map: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: false 
     autoincrement: false 
    title: 
     type: string(500) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    thumbnail: 
     type: string(500) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    WebFotoMap: 
     local: map 
     foreign: id 
     type: one 
    WebNoticia: 
     local: id 
     foreign: foto 
     type: many 
    WebPromocion: 
     local: id 
     foreign: foto 
     type: many 
    WebPromocion_2: 
     class: WebPromocion 
     local: id 
     foreign: flyer 
     type: many 

正如你看到的,我WebPromocion對象有兩個字段引用WebFoto對象(「照片」領域,「傳單」字段) 。我爲WebPromocion寫了一個表格,嵌入WebFoto的一個表格,一個名爲'foto',另一個名爲'flyer'....我用netbeans調試過它,它似乎很好地構建了這些對象,它保存了嵌入對象,但是當它要拯救的WebPromocion,SQL查詢如下:

INSERT INTO WebPromocion (foto, nombre, desde, hasta, descripcion, status, 
flyer) VALUES (?, ?, ?, ?, ?, ?, ?) - (5, prueba, 2011-12-29, 2011-12-29, 
wepale, A, Array) 

調試時,我發現,傳遞給負責執行的函數參數是錯誤的:

exec('INSERT INTO WebPromocion (foto, nombre, desde, hasta, descripcion, status, 
flyer) VALUES (?, ?, ?, ?, ?, ?, ?)', array('5', 'prueba', '2011-12-29', 
'2011-12-29', 'wepale', 'A', array('nombre' => 'radioactivo', 'alt' => 
'radioactivo', 'width' => 100, 'height' => 100, 'title' => 'help!!!', 'maps' => 
array('map' => array('name' => 'map2', 'areas' => array('area_1' => array(
'shape' => 'rect', 'coords' => '0,0,100,100', 'href' => 'google.com', 'alt' 
=> 'google', 'title' => 'google', 'id' => null)), 'id' => null)), 'id' => 
null, 'archivo' => object('sfValidatedFile')))) 

所以,對於第一個外鍵字段('foto'),它放置了正確的值(在這種情況下爲'5',相應的(相關WebFoto的id或主鍵),但對於第二個('傳單'),它放置代表WebFoto對象的數組,而不是其主鍵。

我不知道該怎麼做才能解決這個問題......我嘗試過使用一個空格來嵌入WebFotoForm s,並將其嵌入到WebPromocionForm中,但這種方式甚至沒有保存WebFoto對象......我認爲問題可能存在甚至是一個調節問題,而不是有兩個外鍵('foto'和'flyer'),我將不得不有一個多對多的關係......但這只是一個假設,我試圖以避免我的模型的變化...

+0

看起來像問題是你有你的關係配置翻了一番。 Iam不知道我確實遇到過你的問題,但我知道它從來沒有爲我這樣工作。 嘗試僅在一側進行定義。 – palmic 2012-01-06 09:00:47

回答

1

我認爲你的模型它是一個小mes sy(實際上,我認爲它是一些自動生成的schema.yml)。也許有一些信息丟失(如Producto實體)。 這裏一些提示,也許可以幫助你確定你的模型手動有序(假設學說1.2):

  • 不定義IDS列。學說爲你創造bigint,PK和AI。
  • 不要使用名稱爲「_NN」的列或關係。學說在吸氣和吸氣方法上有一些麻煩。
  • 以「_id」結尾您的FK列名稱
  • 僅定義1-n,1-1和n-m關係,n-1關係應在相反的模型實體中定義,並使用foreignAlias進行取反。
  • 1-n關係:定義「name」,「onDelete」,「local」,「foreign」和「foreignAlias」。如果關係的名稱不是被引用的實體模型名稱,則爲該定義添加「類」。記住「name」和「foreignAlias」成爲記錄getter/setter和查詢連接器。
  • 1-1關係:同義詞1-n,但是type =「one」和單數foreignAlias
  • n-m關係:定義「name」,「class」,「refClass」,「local」和「foreign」。將兩個實體中的關係定義爲n-m,以及關係實體中的兩個半1-n關係。在n-m關係中,本地和外部是指向每個模型的關係實體模型列。

您模式將成爲類似:

WebPromocion: 
    columns: 
    nombre: { type: string(100), notnull: true } 
    foto_id: { type: integer, notnull: true } 
    flyer_id: { type: integer, notnull: false } 
    desde: { type: timestamp, notnull: true } 
    hasta: { type: timestamp, notnull: true } 
    description: { type: clob, notnull: false } 
    status: { type: string(1), notnull: true, default: 'X' } # some default value looks great for a status column 
    relations: 
    WebFoto: { onDelete: CASCADE, local: foto_id, foreign: id, foreignAlias: WebPromociones } 
    Flyer: { class: WebFoto, onDelete: SET NULL, local: flyer_id, foreign: id, foreignAlias: WebPromociones } 
    Productos: { class: Producto, refClass: WebPromocionProducto, local: webpromocion_id, foreign: producto_id } 

webFoto: 
    columns: 
    ruta: { type: string(500), notnull: true } 
    archivo: { type: string(150), notnull: true } 
    nombre: { type: string(255), notnull: true, default: '' } 
    width: { type: integer(4), notnull: true } 
    height: { type: integer(4), notnull: true } 
    map: { type: integer(4), notnull: false }  
    title: { type: string(500), notnull: false } 
    thumbnail: { type: string(500), notnull: false } 


Producto: 
    relations: 
    WebPromociones: { class: WebPromocion, refClass: WebPromocionProducto, local: producto_id, foreign: webpromocion_id } 

WebPromocionProducto: 
    columns: 
    producto_id: { type: integer, notnull: true } 
    webpromocion_id: { type: integer, notnull: true } 
    relations: 
    Producto: { onDelete: CASCADE, local: producto_id, foreign: id, foreignAlias: WebPromocionesProductos } 
    WebPromocion: { onDelete: CASCADE, local: webpromocion_id, foreign: id, foreignAlias: WebPromocionesProductos } 
基於關係的名稱( 「:」 左部從)

,與任何$ webPromocion對象,你可以做(​​

$webPromocion->getWebFoto(), ->getFlyer(), ->getProductos(). 

在任何的webPROococion表查詢,你可以做

->innerJoin('wp.WebFoto wf'), ->innerJoin('wp.Flyer'), ->innerJoin('wp.Productos') 

基地在foreignAlias d,與任何$ webFoto對象,你可以這樣做:

$webFoto->getWebPromociones() 

,並在任何webFoto表查詢,你可以做

->innerJoin('wf.WebPromociones') 

有一個很好的的schema.yml這是至關重要的,當你正在開發symfony1.4-doctrine應用程序。 然後,你應該customWebPromocionForm樣子:

class customWebPromocionForm extends WebPromocionForm { 
    public function configure() { 
    parent::configure(); 
    unset($this['foto_id'],$this['flyer_id']); 
    $foto_object = $this->getObject()->getWebFoto(); 
    $flyer_object = $this->getObject()->getFlyer(); 
    $this->embedForm('foto_form', new customWebFotoForm($foto_object)); 
    $this->embedForm('flyer_form', new customWebFotoForm($flyer_object)); 
    // of course you should define customWebFotoForm, o simply use the default webFotoForm 
    } 
} 

這就是全部。它在您創建或編輯WebPromocion時起作用。

永遠記住:「懶惰的男人雙倍工作」,o「el vago trabaja doble」。不要使用模式自動生成器。

SFMBE

+0

嗨!好吧,雖然有點晚了...你的回答非常有價值。這裏顯示的模式只是完整模式的一部分...手動編寫它不是一個選項,因爲完整的模式太大了。另外,我還沒有發現關於與Doctrine 1.2建立關係的完整非歧義性文檔(我甚至放棄使用symfony1.4並開始使用Doctrine2的第2版)。這就是爲什麼我欣賞你的答案......你的提示非常有用!不知道我是否曾經使用它們,但是很好,讓它們寫在某處=)謝謝! – Throoze 2012-07-06 07:25:51