Question

I have this M:N setup with some tables. Here's a screenshot of the ERD from MySQL Workbench:

enter image description here

I was generated modules for proyectos, centros and unidades by using doctrine:generate-admin task but now I have a problem: I need to add unidades from centros form and those should save the n:m relation at unidades_has_centros table. I put this in CentrosForm:

public function configure() {
    $this->widgetSchema['unidad'] = new sfWidgetFormDoctrineChoice(array('model' => 'Unidades', 'add_empty' => 'Seleccione una unidad', 'multiple' => true));
    $this->validatorSchema['unidad'] = new sfValidatorPass();
}

And that display the control but when I save data nothing is saved to unidades_has_centros table. This is my schema.yml (generated using doctrine:generate-schema task):

Centros:
  connection: doctrine
  tableName: centros
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    descripcion:
      type: string(250)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    ProyectosHasCentros:
      local: id
      foreign: centros_id
      type: many
    UnidadesHasCentros:
      local: id
      foreign: centros_id
      type: many
Cliente:
  connection: doctrine
  tableName: cliente
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    nombre:
      type: string(45)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    fecha_registro:
      type: timestamp(25)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    pais:
      type: string(45)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    Proyectos:
      local: id
      foreign: cliente
      type: many
Proyectos:
  connection: doctrine
  tableName: proyectos
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    nombre:
      type: string(150)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    pais:
      type: string(45)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    estado:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    cliente:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
  relations:
    Cliente:
      local: cliente
      foreign: id
      type: one
    ProyectosHasCentros:
      local: id
      foreign: proyectos_id
      type: many
ProyectosHasCentros:
  connection: doctrine
  tableName: proyectos_has_centros
  columns:
    proyectos_has_centros_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    proyectos_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    proyectos_cliente:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    centros_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
  relations:
    Proyectos:
      local: proyectos_id
      foreign: id
      type: one
    Centros:
      local: centros_id
      foreign: id
      type: one
Unidades:
  connection: doctrine
  tableName: unidades
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    descripcion:
      type: string(250)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    UnidadesHasCentros:
      local: id
      foreign: unidades_id
      type: many
UnidadesHasCentros:
  connection: doctrine
  tableName: unidades_has_centros
  columns:
    unidades_has_centros_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    unidades_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    centros_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
  relations:
    Unidades:
      local: unidades_id
      foreign: id
      type: one
    Centros:
      local: centros_id
      foreign: id
      type: one

Any advice?

Was it helpful?

Solution

You should also overwrite save method of form. Something like:

public function save(){
  $object = parent::save($con);

  UnidadesHasCentrosPeer::saveItems($this->values['unidad'], $object); 
}

And in saveItem method you should define the logic of creating UnidadesHasCentros objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top