Model view controller 这是处理标记的好方法吗?

Model view controller 这是处理标记的好方法吗?,model-view-controller,coldfusion,refactoring,tagging,cfwheels,Model View Controller,Coldfusion,Refactoring,Tagging,Cfwheels,这个代码可以被优化或重新分解吗?这是标记的最佳方法吗 下面的代码是MyPosts模型中的回调。它创建一条记录,将标签与QuestionsTags joiner表中的帖子相关联。必要时,如果标签表中不存在给定的标签,函数将创建该标签,然后使用其id在QuestionsTags表中创建新记录 这种方法的困难在于QuestionsTags表依赖于tags表中可能存在或不存在的数据 该函数采用以下表格: tags(id, tagName), posts(tags) // Comma delimited

这个代码可以被优化或重新分解吗?这是标记的最佳方法吗

下面的代码是MyPosts模型中的回调。它创建一条记录,将标签与QuestionsTags joiner表中的帖子相关联。必要时,如果标签表中不存在给定的标签,函数将创建该标签,然后使用其id在QuestionsTags表中创建新记录

这种方法的困难在于QuestionsTags表依赖于tags表中可能存在或不存在的数据

该函数采用以下表格:

tags(id, tagName),
posts(tags) // Comma delimited list
questionsTags(postId, tagId)
其思想是循环使用post提交的标记的分隔列表,并检查每个标记是否已经存在于标记表中

如果标签存在:

  • 检查此帖子和问号表中的问号是否已有问号记录
  • 如果是,则不执行任何操作(关联已存在)
  • 如果否,则使用现有标签的id和发布的id创建新的问题标签记录
  • 如果标签不存在:

  • 在标记表中创建新标记
  • 使用其id创建新的问题记录
  • 代码


    仅供参考:我在我的应用程序中使用了CFWheels,这解释了使用的ORM函数。

    这正是我处理它的方式。好奇你为什么用“,”作为分隔符?如果“用户没有离开一个空间”怎么办?我只使用逗号和trim()作为列表元素

    此外,您还可以在for()循环中使用local.i++。哦,为什么您要接触“this”范围,而不是将参数传递给函数并引用“arguments.tagList”或类似的内容?从最佳实践的角度来看,“this”范围的使用有点不受欢迎。我还对您在列表中引用“local.I”作为标记项感到困惑。您不需要执行listGetAt(列表,i)来获取实际的标记吗?我只是这个例子中的迭代器,不是吗?我想说的另一件事是,在处理之前,您可以考虑首先清除所有现有的关联。我以前可能已经标记了一些东西,但现在我正在更改标记,这样你就不想在对象上留下任何旧的关联(或者你呢?)
    /**
    * @hint Sets tags for a given question.
    **/
    private function setTags()
    {
        // Loop over the comma and space delmited list of tags
        for (local.i = 1; local.i LTE ListLen(this.tags, ", "); local.i = (local.i + 1))
        {
            // Check if the tag already exists in the database
            local.tag = model("tag").findOneByTagName(local.i);
            // If the tag exists, look for an existing association between the tag and the question in the QuestionTag table
            if (IsObject(local.tag))
            {
                local.questionTag = model("questionTag").findOneByPostIdAndTagId(values="#this.postId#,#local.tag.id#");
                // If no assciatione exists, create a new QuestionTag record using the tagId and the postId
                if (! IsObject(local.questionTag))
                {
                    local.newQuestionTag = model("questionTag").new(postId = this.id, tagId = local.tag.id);
                    // Abort if the saving the new QuestionTag is unsuccesful
                    if (! local.newQuestionTag.save())
                    {
                        return false;
                    }
                }
            }
            // If the tag does not exist create it
            else
            {
                local.newTag = model("tag").new(tagName = local.i, userId = this.ownerUserId);
                // Abort if the the new tag is not saved successfully
                if (! local.newTag.save())
                {
                    return false;
                }
    
                // Otherwise create a new association in the QuestionTags table using the id of the newly created tag and the postId
                local.newQuestionTag = model("questionTag").new(postId = this.id, tagId = local.newTag.id);
                // Abort if the new QuestionTag does not save correctly
                if (! local.newQuestionTag.save())
                {
                    return false;
                }
            }
        }
    }