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 fa开发者_开发知识库ils (
eat/alumno__beca
, CONSTRAINTalumno__beca_persona_id_alumno_id
FOREIGN KEY (persona_id
) REFERENCESalumno
(id
) ON DELETE CASCADE)
Does someone know how to write a fixture for a table inherited from another?
Thanks!
I started symfony in last weeks and i have problem today with these..
first problem is, your foreign keys must be integer. not integer(3).. should not write fields size if it is a foreign key. And also your code is correct, this is just an info.
second problem is, you should remove that foreign key indexes on your database table. if it is already created, when you insert-sql again it gives an error.
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.
精彩评论