多对多与没有复合键的cfwheel的关系 [英] Many to Many relationship with cfwheels without composite keys

查看:182
本文介绍了多对多与没有复合键的cfwheel的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直关注这里的信息:
cfwheels.org/docs/1-1/chapter/nested-properties

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

代码执行正常,在我没有错误的情况下,但是many-many表不会获得新的条目,当我在数据库中手动添加条目时,它们不会反映复选框,有时它们会在更新模型时移除。

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.

我发现了问题...只是没有如何解决它。这里有一个小细节,很容易错过。应用程序似乎依赖于组合键和键的顺序很重要。但我不使用复合键。

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.

(以下 https://github.com/mhenke/cfwheels-training/blob/develop/03-tags.md 作为示例...)

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

如何获取包含cols的表: id tagsid commentsid 可以工作吗?

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

我看到的问题是,cfwheels在创建taggings时一直尝试使用id标签模型

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

推荐答案

尽管我喜欢CFWheels,但我不得不承认我不是形式助手函数的粉丝, 快捷功能。在这个例子中,我只是恢复到更直接/简单的CFML来构造复选框(如果不是整个窗体)和循环逻辑来保存连接表中的值。例如:

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.

这篇关于多对多与没有复合键的cfwheel的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆