1

I've got a model that has a nested property, which has more nested properties.

Let's say house hasMany pets has many legs

It works easily enough when there is just one level, but with two, I'm getting an error when deleting the pets, because the petsID is a foreign key in the legs table, and cfwheels is not deleting it first. I know I could just do the deletion myself with an extra command, but I'm wondering if there is a setting I missed that allows this kind of deletion

Daniel
  • 34,125
  • 17
  • 102
  • 150

1 Answers1

1

Did you try using the dependent argument on the association definitions?

// In `House.cfc`
hasMany(name="pets", dependent="delete");

// In `Pet.cfc`
hasMany(name="legs", dependent="delete");

When you call house.delete(), it should delete any associated pets, which then would delete any associated legs as well.

See hasMany() documentation and the "Dependencies" section of the Associations chapter.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • On a side note, you could change the `dependent` to `deleteAll` on the `legs` association if you don't need to instantiate the `Leg` objects for any callbacks or dependencies. You would need to keep the `pets` association at `delete` though because it would need to instantiate those objects to run the `delete` dependency. Hope that makes sense! – Chris Peters Jun 27 '12 at 11:38
  • Thanks Chris, I see now where I went wrong. I was using `deleteAll` instead of `delete` – Daniel Jun 27 '12 at 15:30