Pergunta

I am using the Eloquent plymorphic relations and the ON DELETE CASCADE option of mysql, and am afraid of how my data will stay consistent over time. Let me explain:

If I have a model Room which contains furnitures of several types (morphMany of Table, Chair, Couch...), and that I decide to delete it, his related furnitures won't get deleted.

To solve this issure I handled the deletion of the related furnitures in the delete event of the Room model, but this event won't necessarily get fired all the time.

Let's say that I have an other model called House, containing several rooms (hasMany). If I decide to delete it, its related models will deleted by mysql with the ON DELETE CASCADE option. I will then stay with data in my tables tables, chairs, couchs that are not relevant anymore, and not related to any row of the rooms table anymore.

So what is the best way to keep a database consistent with ON DELETE CASCADE and the Eloquent's polymorphic relations?

Foi útil?

Solução

If you're using the raw database (cascading) approach, that bypasses Eloquent and it becomes impossible to do what you want.

Set up a deleted event for House which iterates through all its Rooms and deletes those, which again triggers the Room's deleted events.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top