CakePHP 在应该插入 HasAndBelongsToMany 模型时正在更新

发布于 2024-08-21 09:18:58 字数 2083 浏览 3 评论 0原文

我有一个小问题。我正在制作一个带有标签和问题的网站。我有一个 Question 模型、Tag 模型、QuestionsTag 模型,一切都很好地组合在一起。用户在提出问题时将标签放入由空格 (foo bar baz) 分隔的字段中,就像在 stackoverflow.com 上一样。

现在,这里是检查标签是否已存在并将标签输入数据库和所需关联的代码:

    function create () {
        if (!empty($this->data)) {
            $this->data['Question']['user_id'] = 1;
            $question = $this->Question->save ($this->data);

            /**
            * Preverimo če se je vprašanje shranilo, če se je,
            * vprašanje označimo.
            */
            if ($question) {
                $tags = explode (' ', $this->data['Question']['tags']);
                foreach ($tags as $tag){
                    if (($tagId = $this->Tag->existsByName($tag)) != false) {
                        /**
                        * Značka že obstaja, torej samo povezemo trenuten
                        * id z vprašanjem
                        */
                        $this->QuestionsTag->save (array(
                            'question_id' => $this->Question->id,
                            'tag_id'      => $tagId
                        ));
                    }
                    else {
                        /**
                        * Značka še ne obstaja, jo ustvarimo!
                        */
                        $this->Tag->save (array(
                            'name' => $tag
                        ));

                        // Sedaj pa shranimo
                        $this->QuestionsTag->save(array(
                            'question_id' => $this->Question->id,
                            'tag_id'      => $this->Tag->id
                        ));
                        $this->Tag->id = false;
                    }
;               }
            }
        }
    }

问题是这样的,一个问题的 id 为 1,我希望它具有 id 的标签1、2、3。

当调用第二次和第三次保存时,Cake 发现 questions_tags 表中已经有一个 id 为 1 的问题,因此它只更新标签。

但这是不正确的,因为该表中应该有许多具有相同 id 的问题,因为它们引用了属于它们的不同标签。

那么,有没有办法防止这种情况发生呢?阻止保存方法更新?

谢谢你!

I have a small problem. I am making a site that has Tags and Questions. I have a Question model, Tag model, QuestionsTag model, everything fits together nicely. The user upon asking something puts the tags in the field seperated by a space (foo bar baz) much like on stackoverflow.com.

Now, here is the code to check if a tag already exists or not and entering the tag into the database and the required associations:

    function create () {
        if (!empty($this->data)) {
            $this->data['Question']['user_id'] = 1;
            $question = $this->Question->save ($this->data);

            /**
            * Preverimo če se je vprašanje shranilo, če se je,
            * vprašanje označimo.
            */
            if ($question) {
                $tags = explode (' ', $this->data['Question']['tags']);
                foreach ($tags as $tag){
                    if (($tagId = $this->Tag->existsByName($tag)) != false) {
                        /**
                        * Značka že obstaja, torej samo povezemo trenuten
                        * id z vprašanjem
                        */
                        $this->QuestionsTag->save (array(
                            'question_id' => $this->Question->id,
                            'tag_id'      => $tagId
                        ));
                    }
                    else {
                        /**
                        * Značka še ne obstaja, jo ustvarimo!
                        */
                        $this->Tag->save (array(
                            'name' => $tag
                        ));

                        // Sedaj pa shranimo
                        $this->QuestionsTag->save(array(
                            'question_id' => $this->Question->id,
                            'tag_id'      => $this->Tag->id
                        ));
                        $this->Tag->id = false;
                    }
;               }
            }
        }
    }

The problem is this, a Question has an id of 1 and I want it to have the tags with id of 1, 2, 3.

When the 2nd and 3rd save get called, Cake sees that in the questions_tags table is already a question with id 1, so it just updates the tag.

But this is not correct, as there should be many questions in that table with the same id, as they refer to different tags belonging to them.

So, is there a way to prevent this? Prevent the save method from UPDATEing?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池予 2024-08-28 09:18:58

此行为并非特定于 HABTM 关系。您正在循环内调用 save() 方法。第一次保存后,会设置一个 id 值,随后的每个保存调用都会看到该 id 并假定它是更新。在循环内,首先需要调用 model->create() 来重置可能存在的 id 值。

来自 CakePHP 文档 http://book.cakephp.org/view/75 /保存您的数据

在循环中调用 save 时,不要忘记调用 create()。

在你的情况下,它看起来像这样:

$this->QuestionsTag->create();
$this->QuestionsTag->save (array(
                        'question_id' => $this->Question->id,
                        'tag_id'      => $tagId
                    ));

This behavior isn't specific to HABTM relationships. You are calling the save() method inside of a loop. After the first save, an id value is set and each subsequent save call sees the id and assumes it's an update. Within a loop, you first need to call model->create() to reset an id value that may exist.

From the CakePHP Docs at http://book.cakephp.org/view/75/Saving-Your-Data:

When calling save in a loop, don't forget to call create().

In your case, it would look like this:

$this->QuestionsTag->create();
$this->QuestionsTag->save (array(
                        'question_id' => $this->Question->id,
                        'tag_id'      => $tagId
                    ));
枕头说它不想醒 2024-08-28 09:18:58

查看saveAll。您可以对 $this->Question->saveAll() 进行一次调用,它也会保存您提供的任何关联数据。请注意,对于 HABTM 数据,它将对与该 question_id 关联的任何 questions_tags 执行 DELETE,然后执行 INSERT > 对于您的数据中包含的所有 tag_id

Check out saveAll. You can make a single call to $this->Question->saveAll(), and it will save any associated data you supply as well. Note that with HABTM data, it will perform a DELETE for any questions_tags associated with that question_id, then perform an INSERT for all the tag_id's included with your data.

仅此而已 2024-08-28 09:18:58

如果您想确保创建一个新条目(INSERT)而不是更新,您可以在保存调用之前设置 $this->create(); 。请参阅http://book.cakephp.org/view/75/Saving-Your -Data(页面上部):循环调用save时,不要忘记调用create()。

if you want to make sure, that a new entry (INSERT) is made rather then an update, you can set $this->create(); right in front of the save call. See http://book.cakephp.org/view/75/Saving-Your-Data (in the upper part of the page): When calling save in a loop, don't forget to call create().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文