Many to Many relationship with cfwheels without composite keys

試著忘記壹切 提交于 2019-12-11 10:37:00

问题


I've been following the information from here: cfwheels.org/docs/1-1/chapter/nested-properties

I've ended up downloading a sample application which breaks down at the same place

code executes fine, in the sense that I get no errors, but the many-many table does not get the new entries, and when I add the entries manually in the database, they are not reflected with the checkboxes and sometimes they are removed when the model is updated.


EDIT

I found out the issue... just not how to solve it. There's a small detail there that is very easy to miss. The application seems to rely on composite keys and the order of the keys matters. But I'm not using composite keys.

(following https://github.com/mhenke/cfwheels-training/blob/develop/03-tags.md as an example...)

How do I get a table with cols: id,tagsid, and commentsid to work?

the problems I see is that cfwheels keeps trying to use the id tag when creating the taggings model


回答1:


As much as I love about CFWheels, I have to admit that I am not a fan of the form helper functions or the "shortcut" feature. In this example case, I'd just "revert" to the more straightforward/simple CFML to construct the checkboxes (if not the whole form) and looping logic to save the values in the join table. For example:

<fieldset>
<legend>PropertyLanguages</legend>

<cfloop query="Languages">
<label>
  #Languages.language#
  <input type="checkbox" name="Property[PropertyLanguages]" value="#Languages.id#">
</label>
</cfloop>


</fieldset>

Then change the update controller logic like so:

<!--- CONTROLLER - update.cfm - updateProperty --->
<cffunction name="updateProperty">
    <cfscript>   
    Property = model("Property").findByKey(key=params.Property.id);
    Property.update(params.Property);

    if (IsDefined("params.Property.PropertyLanguages"))
    {
      model("PropertyLanguages").deleteAll(where="propertyid=#params.Property.id# AND languageid NOT IN (#params.Property.PropertyLanguages#)");

      for (var i = 1; i<=ListLen(params.Property.PropertyLanguages); i++)
      {
        languageid = ListGetAt(params.Property.PropertyLanguages, i);
        if (! IsObject(model("PropertyLanguages").findOne(where="propertyid=#params.Property.id# AND languageid=#languageid#")))
        {
          pl = model("PropertyLanguages").new();
          pl.langugageid = languageid;
          pl.propertyid = params.Property.id;
          pl.save();
        }
      }
    }
    else
    {
      model("PropertyLanguages").deleteAll(where="propertyid=#params.Property.id#");
    }
    </cfscript>    
</cffunction>

I haven't tested this, but it should work, more or less. It's not as simple as it could (should?) be using the wheels helpers, but it doesn't seem too bad.



来源:https://stackoverflow.com/questions/8275830/many-to-many-relationship-with-cfwheels-without-composite-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!