Search code examples
phpsymfony1doctrinesymfony-1.4doctrine-inheritance

Fixtures and inheritance in Symfony


I have a database schema in Symfony like this:

Persona:
    actAs: { Timestampable: ~ }
    columns:
      primer_nombre:  { type: string(255), notnull: true }
      segundo_nombre: { type: string(255) }
      apellido:   { type: string(255), notnull: true }
      rut:         { type: string(255) }
      email:       { type: string(255) }
      email2:      { type: string(255) } 
      direccion:     { type: string(400) }
      ciudad:        { type: string(255) }
      region:      { type: string(255) }
      pais:     { type: string(255) }
      telefono:       { type: string(255) }
      telefono2:      { type: string(255) }
      fecha_nacimiento:   { type: date }

Alumno:
 inheritance:
    type:          concrete
    extends:       Persona
 columns:
  comentario:  { type: string(255) }
  estado_pago: { type: string(255) }

Alumno_Beca:
 columns:
  persona_id:   { type: integer, primary: true }
  beca_id: { type: integer, primary: true }
 relations:
  Alumno: { onDelete: CASCADE, local: persona_id, foreign: id } 
  Beca: { onDelete: CASCADE, local: beca_id, foreign: id } 

Beca:
 columns:
  nombre:        { type: string(255) }
  monto:      { type: double }
  porcentaje:  { type: double }
  descripcion: { type: string(5000) }

As you see, "alumno" has a concrete inheritance from "persona". Now I'm trying to create fixtures for this two tables, and I can't make Doctrine to load them. It gives me this error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (eat/alumno__beca, CONSTRAINT alumno__beca_persona_id_alumno_id FOREIGN KEY (persona_id) REFERENCES alumno (id) ON DELETE CASCADE)

Does someone know how to write a fixture for a table inherited from another?

Thanks!


Solution

  • I'm not sure if this is your only problem, but when loading fixtures for tables that have relationships, load them individually in an order that does not violate their foreign key constraints: first parent, then child... not everything together. A child record cannot "legally" exist without its parent, if you have defined your relationships in this way, or else the constraint fails.