CakePHP beforeSave & 保存前HABTM

发布于 2024-11-29 06:16:33 字数 1646 浏览 1 评论 0原文

我正在尝试在 CakePHP 2.0 beta 中编写一些 beforeSave 逻辑。基本上我希望用户能够提交一些文本以实现类似标签的功能。 beforeSave 函数将搜索相关表,如果标签存在,则将其链接到主记录,如果不存在,则创建新标签并保存。然后链接它。

这是我的函数(在 beforeSave 中):

function beforeSave() {
    if(!empty($this->data['Term']) && isset($this->Term)){
       $terms = $this->Term->find('list');
       $terms = array_flip($terms);

       foreach($this->data['Term'] as $key => $term){
           if(!empty($terms[$term['name']])){
               $this->data['Term']['Term'][$key]['id'] = $terms[$term['name']];
               unset($this->data['Term'][$key]);
           }else{
               $this->Term->create();
               $this->Term->save(array('Term' => array('name' => $term['name'])));
               $this->data['Term']['Term'][$key]['id'] = $this->Term->id;
               unset($this->data['Term'][$key]);
           }
       }
   }

   return true;
}

这基本上按照我想要的方式工作,在必要时创建记录并查找现有记录,创建一个像这样的数组:

Array
(
    [Project] => Array
        (
            [id] => 2
            [title] => Project Title
            ...
        )

    [Term] => Array
        (
            [Term] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                        )

                    [1] => Array
                        (
                            [id] => 2
                        )

                )

        )

)

它还成功地将数据保存在主模型(项目)中。但 [Term][Term] 下的关联将被忽略。我非常确定这是保存 HABTM 关联的正确数组结构。

有人看出这有什么问题吗?

I'm trying to write some beforeSave logic in CakePHP 2.0 beta. Basically I want the user to be able to submit some text for tag-like functionality. The beforeSave function would search a related table, if the tag exists just link it to the primary record, and if not, create the new tag & then link it.

So here's my function (in beforeSave):

function beforeSave() {
    if(!empty($this->data['Term']) && isset($this->Term)){
       $terms = $this->Term->find('list');
       $terms = array_flip($terms);

       foreach($this->data['Term'] as $key => $term){
           if(!empty($terms[$term['name']])){
               $this->data['Term']['Term'][$key]['id'] = $terms[$term['name']];
               unset($this->data['Term'][$key]);
           }else{
               $this->Term->create();
               $this->Term->save(array('Term' => array('name' => $term['name'])));
               $this->data['Term']['Term'][$key]['id'] = $this->Term->id;
               unset($this->data['Term'][$key]);
           }
       }
   }

   return true;
}

This basically works the way I want, creating records where necessary and finding the existing records, creating an array like this:

Array
(
    [Project] => Array
        (
            [id] => 2
            [title] => Project Title
            ...
        )

    [Term] => Array
        (
            [Term] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                        )

                    [1] => Array
                        (
                            [id] => 2
                        )

                )

        )

)

It also successfully saves the data in the primary model (Project). But the associations under [Term][Term] are ignored. I'm pretty sure this is the correct array structure to save HABTM associations.

Anyone see what is wrong with this?

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

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

发布评论

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

评论(1

栀子花开つ 2024-12-06 06:16:33

在视图中,您应该有 1 个输入文本 $form->input('terms'); 以便用户可以输入所有标签。因此,在 beforeSave 中,您将拥有一串标签(或术语),您可能希望将其切片为数组,find('list') 术语以及一些数组相交或其他内容。但最终,您需要数据看起来像这样才能使用 saveAll:

<前><代码>数组

[项目] =>大批

[id] => 2
[标题] =>项目名称
...

[术语] =>大批

[术语] =>大批

[0] => 10 // 标签的id
[1] => 2

)

in the view you should have 1 input text $form->input('terms'); so the user can enter all the tags. So in beforeSave, you would have a string of the tags (or terms), you might want to slice it out to array, find('list') of the terms, and some array intersect or something. But in the end, you need the data to look something like this to use saveAll:

Array
(
[Project] => Array
    (
        [id] => 2
        [title] => Project Title
        ...
    )
[Term] => Array
    (
        [Term] => Array
            (
                [0] => 10 // id of the tag
                [1] => 2
            )
    )

)

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