coldfusioncoldfusion-9cfwheels

cfwheels nested properties issue


I'm trying to get this to work...

I have a five tables that I"m trying to tie together: properties, languages, propertyLanguages, buildings and buildingTranslations

properties,languages and propertylanguages is a typical many-to-many relationship that I have working. What I'm trying to do next is have the buildings, which are linked to the property and have text fields for each language that will go inside the buildingtranslations.

I've setup the foreign keys for the propertylanguages, buildings and buildingtranslations

I'm just not sure how to setup the model and the controller when creating/updating building records


edit

I've managed to create a view in mssql that represents the relationship view

hopefully this makes it easier to see the relationships.

I want to create and edit Buildings with the translation fields included (and updated in the database)

The languages are assigned at the property level. The building that is linked to the property through the propertyid uses the languages available (through propertylanguages[where propertyid = building.propertyid]) to determine the buildingTranslations required for the building


Solution

  • Hopefully this helps some:

    models/Building.cfc

    hasMany(name="BuildingTranslations", foreignKey="yrhBuildingId");
    belongsTo(name="Property", foreignKey="yrhPropertyId");
    

    controllers/Buildings.cfc

    function new () {
      building = model("Building").new();
      building.yrhPropertyId = params.yrhPropertyId; //assuming this was passed in
      requiredLanguages = model("PropertyLanguages").findAll(where="yhrPropertyId=#building.yhrPropertyId#");
    }
    
    function create () {
      building = model("Building").new(params.Building);
      building.save();
    
      requiredLanguages = model("PropertyLanguages").findAll(where="yhrPropertyId=#building.yhrPropertyId#");
      for (var i = 1; i <= requiredLanguages.recordCount; i++)
      {
        buildingTranslation = model("BuildingTranslation").new();
        buildingTranslation.yrhBuildingId = building.id;
        buildingTranslation.yrhLanguageId = requiredLanguages.yrhLanguageId[i];
        buildingTranslation.langName = params.BuildingTranslations[requiredLanguages.yrhLanguageId[i]];
        buildingTranslation.save();
      }
    
      redirectTo(action="list");
    }