嵌入表单不保存正确

问题描述:

我有以下型号:嵌入表单不保存正确

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

我认为你的模型它是一个小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