symfony中的嵌入式表单1.4不能正常保存 [英] Embedded forms in symfony 1.4 not saving propperly
问题描述
我有以下模型:
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,另一个称为传单'....我已经用netbeans调试了它,似乎构造好的对象,它保存嵌入的对象,但是当它将保存 WebPromocion
,sql查询如下:
As you can see, my WebPromocion
object has two fields referencing WebFoto
objects ('foto' field, and 'flyer' field). Im writing a form for WebPromocion
, embedding two forms for WebFoto
, one called 'foto' and the other called 'flyer'.... I have debugged it with netbeans, and it seems to construct well the objects, it save the embedded objects, but when it is going to save the the WebPromocion
, the sql query is the following:
INSERT INTO WebPromocion (foto, nombre, desde, hasta, descripcion, status,
flyer) VALUES (?, ?, ?, ?, ?, ?, ?) - (5, prueba, 2011-12-29, 2011-12-29,
wepale, A, Array)
在调试时,我发现传递给负责执行的函数的参数是错误的:
While debugging, i found that arguments passed to the function responsible of executing were wrong:
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或主键),但对于第二个('flyer '),它将数组表示 WebFoto
对象,而不是其主键...
so, for the first foreign key field ('foto'), it places the correct value ('5' in this case, which corresponds to the id or primary key of the related WebFoto
), but for the second one ('flyer'), it places the array representing the WebFoto
object, instead of its primary key...
我不知道该怎么办来解决这个问题...我已经尝试使用一个空的表单来嵌入 WebFotoForm
,并将这个嵌入到 WebPromocionForm
,但是这样它甚至不会保存 WebFoto
对象...我认为问题可能甚至是模型化问题,有两个外键('foto'和'flyer'),我必须要有多对多的关系...但这只是一个假设,我试图避免我的模型的变化.. 。
I don't know what to do to fix this... I have tried using an empty form to embed both WebFotoForm
s, and embedding this one in the WebPromocionForm
, but this way it doesn't even save the WebFoto
objects... I think the problem may be even a modelation problem, that instead of having the two foreign keys ('foto' and 'flyer'), i would have to have a many-to-many relationship... but that's just an assumption , and I'm trying to avoid changes in my model...
任何建议都将非常感谢!感谢你的时间!
Any suggestion would be really appreciated!!! Thanks for you time!
推荐答案
我认为你的模型有点混乱(实际上,我认为这是一些自动生成的schema.yml) 。也许有一些信息丢失(如Producto实体)。
这里有一些提示,可能可以帮助您手动和有序地定义您的模型(假设Doctrine 1.2):
I think your model it's a little messy (actually, I think it's some autogenerated schema.yml). Maybe there's some info missing (as Producto entity). Here some tips that maybe can help you define your model manually and orderly (assuming Doctrine 1.2):
- 不定义ids列。原则为你创建一个bigint,PK和AI。
- 不要使用列或名称结束的关系_NN。学说有一些困惑与吸烟者和setter的方法。
- 使用_id结束您的FK列名称
- 仅定义1-n,1-1和nm关系,n-1关系应为定义在相对模型实体中,并使用foreignAlias反向进行。
- 1-n关系:定义name,onDelete,local,foreign和foreignAlias 。如果关系的名称不是引用的实体模型名称,请为该定义添加类。记住,name和foreignAlias成为记录的getter / setter和查询joiner。
- 1-1关系:idem 1-n,但type =one和singular foreignAlias
- nm关系:definename class,refClass,local和foreign。将两个实体的关系定义为n-m,以及关系实体中的半1-n关系。在nm关系中,本地和外部是指向每个模型的关系实体模型列。
- dont define ids columns. Doctrine create it for you as an bigint, PK and AI.
- dont use columns or relations with name ended "_NN". Doctrine has some troubble with the getter and setter methods.
- end your FK column name with "_id"
- define only 1-n, 1-1 and n-m relations, n-1 relations should be defined in the opposite model entity, and use the foreignAlias for accesing inversely.
- 1-n relation: define "name", "onDelete", "local", "foreign" and "foreignAlias". If the name of the relation it's not the referred entity model name, add the "class" for that definition. Remember that "name" and "foreignAlias" become the record getter/setter and the query joiner.
- 1-1 relation: idem 1-n, but with type="one" and singular foreignAlias
- n-m relation: define "name", "class", "refClass", "local" and "foreign". Define the relation in both entities as n-m, and both semi 1-n relation in the relation-entity. In the n-m relation, local and foreign are the relation-entity model columns that point to each model.
您的模型将成为:
You model will become something like:
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对象(
based on the relations name (the left part from ":"), with any $webPromocion object you can do (
$webPromocion->getWebFoto(), ->getFlyer(), ->getProductos().
可以执行
->innerJoin('wp.WebFoto wf'), ->innerJoin('wp.Flyer'), ->innerJoin('wp.Productos')
基于foreignAlias,可以使用任何$ webFoto对象:
based on the foreignAlias, with any $webFoto object you can do:
$webFoto->getWebPromociones()
并且在任何webFoto表查询中,您可以执行
and in any webFoto table query, you can do
->innerJoin('wf.WebPromociones')
当您开发symfony1.4-doctrine应用程序时,有一个很好的schema.yml是至关重要的。
然后,您的customWebPromocionForm应该如下所示:
Having a nice schema.yml it's critical when you are developing symfony1.4-doctrine apps. Then, your customWebPromocionForm should looks like:
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时,它可以工作。
That's all. It works when you are creating or editing some WebPromocion.
永远记住:懒惰的人双重工作,oel vago trabaja doble。不要使用模式自动发生器。
Always remember: "the lazy man works doubly", o "el vago trabaja doble". Don't use schema autogenerators.
SFMBE
这篇关于symfony中的嵌入式表单1.4不能正常保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!