Question

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

Was it helpful?

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");
}

OTHER TIPS

here's the approach I've taken

    <cfset viewBuildingNames = model("yrhBuildingNamesView").findAll(where="yrhBuildingId=#params.key#")>   <!--- FIND ALL BUILDING NAMES --->
    <cfset yrhbuilding = model("Yrhbuilding").findByKey(key=params.key)>   <!--- CREATE BUILDING MODEL --->
    <cfset yrhproperty = model("YrhProperty").findByKey(key=yrhbuilding.yrhPropertyId, include="YrhPropertyLanguages")>   <!--- language info through property--->
    <cfset yrhbuilding.yrhproperty = yrhproperty>
    <cfset yrhbuilding.yrhBuildingTranslations = ArrayNew(1)>

    <cfloop query="viewBuildingNames">
        <cfset yrhBuildingTranslation = model("yrhBuildingTranslation").new(yrhBuildingId=#yrhBuildingId#, yrhLanguageId=#yrhLanguageId#, langName=#LANGNAME#)>
        <cfset ArrayAppend(yrhbuilding.yrhBuildingTranslations, yrhBuildingTranslation)>
    </cfloop>

it's sort of half cfwheels way. it relies on the view created in the database

once the mode; is created, the updating works, but I don't get an error message on the empty langNames, just an error, Which I can live with.

I'm planning on adding another layer of items residing under the building, that will require the same connection to the propertyLanguages, While it should still work OK I'm getting increasingly queasy about the cfwheels magic taking care of these things. I might be switching to handling complex relationships directly.

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