Question

I've got an issue with a nested property that uses a composite key.

When I'm editing a model that has multiple instances of nested properties (with a composite key) and want to update it to have fewer by leaving them blank, cfWheels does not remove the ones that are not used anymore, and maintains the old value. Is there a way of forcing the deletion of these without calling delete on the nested model?

I've been doing by deleting all nested properties, and then update() creates the records that are needed, but the big issue with that is that when I have code in between that fails, It just deletes the items, which as you know can be very very bad.

Was it helpful?

Solution

In your init call to nestedProperties(), try adding the allowDelete option:

nestedProperties(association="comments", allowDelete=true);

Then if a model within that collection has a property called _delete that is set to true, CFWheels will delete that record.

I'm not sure of your model because you don't include any details in your question, but you could probably run a beforeValidationOnUpdate callback that checks criteria on the nested models and sets _delete = true when the record needs to be deleted.

For example:

// Post.cfc
component extends="Model" {
  function init() {
    hasMany("comments");
    nestedProperties(association="comments", allowDelete=true);
    beforeValidationOnUpdate("removeBlankComments");
  }

  private function removeBlankComments() {
    if (StructKeyExists(this, "comments") && IsArray(this.comments)) {
      for (local.i = 1; local.i < ArrayLen(this.comments); local.i++) {
        if (!Len(this.comments[local.i].message)) {
          this.comments[local.i]._delete = true;
        }
      }
    }
  }
}

Not sure if this will give you any problems with the nested composite key. Sometimes nested properties are a little kludgy with "special" cases.

OTHER TIPS

i think you forgot to mention allowDelete attribute in nestedProperties by defalut allowDelete is set as false in wheels and does not delete the composite key form table. you have to set it true. for example in model you have to do some thing like this.

   <cfset hasMany(name="campaignlanguages",shortcut="languages", dependent="deleteAll") />
   <cfsetnestedProperties(associations="campaignlanguages",allowDelete="true")/>

you can find more details here

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