1
votes

I have the following model:

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

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'))))

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

I don't know what to do to fix this... I have tried using an empty form to embed both WebFotoForms, 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...

1
Looks like the problem is you have your relations configurations doubled. Iam not sure i was experienced your problem exactly, but i know it never worked for me this way. Try to define on one side only.palmic

1 Answers

1
votes

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):

  • 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 }

based on the relations name (the left part from ":"), with any $webPromocion object you can do (

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

in any webPromocion table query, you can do

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

based on the foreignAlias, with any $webFoto object you can do:

$webFoto->getWebPromociones()

and in any webFoto table query, you can do

->innerJoin('wf.WebPromociones')

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
  }
}

That's all. It works when you are creating or editing some WebPromocion.

Always remember: "the lazy man works doubly", o "el vago trabaja doble". Don't use schema autogenerators.

SFMBE