简体   繁体   English

cakephp habtm关系(保存数据)

[英]cakephp habtm relationship (saving data)

Question related to HABTM has been posted in some good numbers on stackoverflow but I am still looking for a solution to my problem. 与HABTM相关的问题已经在stackoverflow上发布了很多,但我仍在寻找解决方案。

I am creating an application that allows for creation of topics based on a particular subcategory. 我正在创建一个应用程序,该应用程序允许基于特定的子类别创建主题。 While adding a topic the user is asked for tags (on the same form). 在添加主题时,系统会要求用户提供标签(以相同的形式)。 Now when the user has finished adding tags to the topic on the click of Add button the topic is successfully added but tags are not. 现在,当用户单击“添加”按钮完成将标签添加到主题后,主题已成功添加,但标签未添加。

I have created the join table for both topics and tags as tbl_tags_topics. 我已经为主题和标签创建了连接表,并将其作为tbl_tags_topics. (as defined in the conventions of cakephp) and defined the 'hasAndBelongsToMany' array properly in both the models of topics and tags. (按照cakephp的约定定义),并在主题和标签模型中正确定义了“ hasAndBelongsToMany”数组。

What steps am I missing now.? 我现在缺少什么步骤?

I have one more question related to this but I will post it when I will be able to send tags related to a topic successfully to the database. 我还有一个与此相关的问题,但是当我能够将与某个主题相关的标签成功发送到数据库时,我将发布该问题。 (the functionality is similar to that of stackoverflow's tags adding and attaching) (该功能类似于stackoverflow的标签添加和附加的功能)

any help is greatly appreciated., also let me know of any good tutorials on HABTM if you find one. 非常感谢您的帮助。如果您找到HABTM的任何优秀教程,也请让我知道。

Thanks 谢谢

When you save one of the fields, you pass parameters like that : 保存其中一个字段时,您将传递如下参数:

$this->Model->save(
    'Model' => array('id' => 1, 'name' => 'one random field)
);

If you have tags, which is a HABTM, you could do the following : 如果具有HABTM标签,则可以执行以下操作:

$this->Model->save(
    'Model' => array('id' => 1, 'name' => 'one random field'),
    'Tag' => array('Tag' => array(1))
);

However there's a problem with this native feature, it's that every time you save your object, you need to pass every tag to the saved array. 但是,此本机功能存在一个问题,那就是每次保存对象时,都需要将每个标签传递给保存的数组。 Otherwise, they'll all be removed before to be readded. 否则,将它们全部删除,然后再读取。

However you can find on bakery, a plugin called extended associations . 但是,您可以在面包店上找到一个名为扩展关联的插件。

Using it, you would do : 使用它,您可以:

$this->Model->habtmAdd('Tag', 1, 1);

Where the first "1" is the model's object id. 其中第一个“ 1”是模型的对象标识。 And the second is the tag's object id. 第二个是标签的对象ID。

And to remove a tag : 并删除标签:

$this->Post->habtmDelete('Tag', 1, 1); 

With the help of Mr. Stornvig , I was able to solve my problem. 斯托恩维格先生的帮助下,我得以解决我的问题。 Here is the link that describes the complete procedure of how to achieve such functionality in cakephp. 这里的链接描述了如何在cakephp中实现此类功能的完整过程。 This is an awesome tutorial for learning more about HABTM relationship. 这是一个很棒的教程,用于学习有关HABTM关系的更多信息。

For version 1.3 I found out this technique to be useful. 对于1.3版,我发现此技术很有用。 Here are the steps: 步骤如下:

after defining the HABTM relationship array in both the models 在两个模型中都定义了HABTM关系数组之后

create your form like this 这样创建您的表单

echo $form->create('Job');
echo $form->input('title');
echo $form->input('description');
echo $form->input('location');
echo $form->input('Category');//note the caps and single plural
$form->end('Submit');

and then in the controller add method simply use the 然后在控制器的add方法中,只需使用

saveAll($this->data) saveAll($ this-> data)

and the join table will be populated with the required records also. 并且联接表也将填充所需的记录。

more info 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM