如何在CakePHP中将数据写入HABTM连接表的HABTM关联? [英] How do I write data to a HABTM association of a HABTM join table in CakePHP?

查看:104
本文介绍了如何在CakePHP中将数据写入HABTM连接表的HABTM关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保存具有以下结构的数据:

I'm trying to save data with following structure:

如您所见,在用户体验表之间有HABTM关联。以及 experiences_users 标签之间的另一个HABTM。我创建了以下形式:

As you can see, there is HABTM association between users and experiences table. And another HABTM between experiences_users and tags. I created following form:

<?php echo $form->create('Experience', array('action' => 'addClassic'));?>
    <?php
    echo $form->input('Experience.date', array('dateFormat' => 'DMY'));
    echo $form->input('Experience.time', array('timeFormat' => '24', 'empty' => array(-1 => '---'), 'default' => '-1'));
    echo $form->input('Experience.name');
    echo $form->input('ExperiencesUser.1.note');
    echo $form->input('ExperiencesUser.1.rating'); 
    //echo $form->input('Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
    //echo $form->input('ExperiencesUser.1.Tags', array('multiple' => 'multiple', 'options' => $tags));
    //echo $form->input('ExperiencesUser.1.Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
    echo $form->input('ExperiencesUser.1.confirmed', array('type' => 'hidden', 'value' => '1'));

    echo $form->input('ExperiencesUser.1.user_id', array('type' => 'hidden'));
    echo $form->input('ExperiencesUser.2.user_id', array('type' => 'hidden'));
    ?>
<?php echo $form->end(__('Add', true));?>

一切都很好。 saveAll 方法创建新体验,向两个用户(通过 experiences_users )提供这种新体验,并设置相关内容。

And everything works well. saveAll method creates new Experience, assings this new experience to two users (via experiences_users) and sets the stuff around.

让我感到烦恼的是,我想为新创建的 experiences_users 记录(第一个)分配一些标签。我想,应该通过一些评论的东西。此注释代码的每一行都创建表单,并将数据发送到 $ this-> data ,但它从不保存。

What bothers me is that I want to assign some Tags to newly created experiences_users record (to the first one). I thought, that should be done via some of the commented stuff. Every line of this commented code creates form and sends data to $this->data, but it never gets saved.

:在我的示例中,将数据保存到 experiences_users_tags 中, $ this-> data $ form-> input 的正确语法是什么?

So my question is: What is the right syntax of $this->data or $form->input in my example to save data into experiences_users_tags?

推荐答案

我想出来了。 Cake saveAll 函数仅适用于直接关联的模型。幸运的蛋糕能够照顾这:

I figured it out. Cake saveAll function works only for directly associated models. Luckily Cake is able to take care of this:

echo $form->input('Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));



控制器



controller

$this->Experience->saveAll($data, $parameters);
$this->Experience->ExperiencesUser->save($data);

调用 saveAll()后填写填写 $ this->数据与最后插入的ID。因此,第二次调用 save()会将数据保存到右侧表格中,并设置正确的外键。

Cake after calling saveAll() fills $this->data with last inserted id. So second call save() will save data to the right table and set properly foreign keys.

这篇关于如何在CakePHP中将数据写入HABTM连接表的HABTM关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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