【问题标题】:Embedded forms in symfony 1.4 not saving properlysymfony 1.4 中的嵌入式表单无法正确保存
【发布时间】:2011-12-29 20:37:30
【问题描述】:

我有以下型号:

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 对象的字段(“foto”字段和“flyer”字段)。我为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对象的数组,而不是它的主键......

我不知道该怎么做才能解决这个问题...我尝试使用一个空表单来嵌入WebFotoForms,并将这个嵌入WebPromocionForm,但这样它甚至没有保存WebFoto 对象...我认为这个问题甚至可能是一个建模问题,而不是拥有两个外键('foto' 和'flyer'),我必须有一个多对多的关系...但这只是一个假设,我试图避免改变我的模型...

【问题讨论】:

  • 看起来问题是你的关系配置加倍了。我不确定我是否完全经历过您的问题,但我知道它从来没有以这种方式对我有用。尝试仅在一侧定义。

标签: php mysql doctrine symfony-1.4 symfony-forms


【解决方案1】:

我认为您的模型有点混乱(实际上,我认为它是一些自动生成的 schema.yml)。也许缺少一些信息(作为 Producto 实体)。 这里有一些提示可能可以帮助您手动有序地定义模型(假设 Doctrine 1.2):

  • 不要定义 ids 列。 Doctrine 为您创建了一个 bigint、PK 和 AI。
  • 不要使用名称以“_NN”结尾的列或关系。 Doctrine 在 getter 和 setter 方法上有一些问题。
  • 以“_id”结束您的 FK 列名称
  • 只定义 1-n、1-1 和 n-m 关系,n-1 关系应该定义在相反的模型实体中,并使用 foreignAlias 进行反向访问。
  • 1-n 关系:定义“name”、“onDelete”、“local”、“foreign”和“foreignAlias”。如果关系的名称不是引用的实体模型名称,请为该定义添加“类”。请记住,“name”和“foreignAlias”成为记录获取器/设置器和查询连接器。
  • 1-1 关系:同上 1-n,但 type="one" 和单数 foreignAlias
  • n-m 关系:定义“name”、“class”、“refClass”、“local”和“foreign”。将两个实体中的关系定义为 n-m,并将关系实体中的两个半 1-n 关系定义为。在 n-m 关系中,local 和 foreign 是指向每个模型的关系实体模型列。

你的模型会变成这样:

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().

在任何 webPromocion 表查询中,您都可以这样做

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

基于foreignAlias,您可以使用任何$webFoto 对象:

$webFoto->getWebPromociones()

在任何 webFoto 表查询中,您都可以这样做

->innerJoin('wf.WebPromociones')

在开发 symfony1.4-doctrine 应用程序时,拥有一个不错的 schema.yml 非常重要。 然后,您的 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 时起作用。

永远记住:“懒惰的人会加倍工作”,或者“el vago trabaja doble”。不要使用模式自动生成器。

SFMBE

【讨论】:

  • 嗨!好吧,虽然有点晚了……你的回答很有价值。这里显示的模式只是完整模式的一个 sn-p...手动编写它不是一种选择,因为完整的模式太大了。另外,我还没有找到关于创建与 Doctrine 1.2 关系的完整且明确的文档(我什至放弃了使用 symfony1.4 并从使用 Doctrine2 的版本 2 开始)。这就是为什么我感谢您的回答...您的提示非常有用!不知道我是否可以使用它们,但最好把它们写在某个地方 =) 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
相关资源
最近更新 更多