简体   繁体   English

将HABTM记录保存到数据库CakePHP

[英]Save a HABTM record to database CakePHP

I am working on a followers / following system with CakePHP 2. I have setup my database with a users table, and a user_users table. 我正在使用CakePHP 2从事关注者/关注系统。我已经使用users表和user_users表设置了我的数据库。 The users table is the main table containing every user on the system, whilst the user_users table contains the records of followers. users表是包含系统上每个用户的主表,而user_users表包含关注者的记录。

I then have a UsersController, User model and Follower model. 然后我有一个UsersController,User模型和Follower模型。

I can successful output a button to either say Follow or Following dependent on whether the currently logged in user is following the user of which the profile they are viewing belongs to, however what I am unable to understand how to do, is create new following relationships in the table. 我可以成功输出一个按钮,根据当前登录的用户是否跟随他们正在查看的个人资料所属的用户,然后根据当前登录的用户是否属于他们,但是我无法理解该怎么做,是创建新的关注关系在表中。 In other words, I do not know how to create records in the user_users table. 换句话说,我不知道如何在user_users表中创建记录。

I am not sure where the logic for this should go, and thus what my "Follow" button should point to. 我不确定这个的逻辑应该去哪里,因此我的“跟随”按钮应该指向什么。

This is probably a very simple question, but I am totally stumped. 这可能是一个非常简单的问题,但我完全难过了。 I have tried adding a "follow" action to the UsersController but I cannot get that to work. 我曾尝试向UsersController添加“跟随”操作,但我无法让它工作。

Any help much appreciated, Duncan 任何帮助非常感谢,邓肯

HATBM isn't a good fit in this situation. HATBM不适合这种情况。 From the cookbook : 食谱

HABTM data is treated like a complete set, each time a new data association is added the complete set of associated rows in database is dropped and created again so you will always need to pass the whole data set for saving. HABTM数据被视为一个完整的集合,每次添加新的数据关联时,数据库中的完整关联行集将被删除并再次创建,因此您始终需要传递整个数据集以进行保存。 For an alternative to using HABTM see hasMany through (The Join Model) 有关使用HABTM的替代方法,请参阅hasMany through(连接模型)

For this reason, HABTM is mainly good for pretty 'dumb' relationships. 出于这个原因,HABTM主要适用于非常“愚蠢”的关系。 I've used it in cases such as where a User has to select many Interests - and they just get a list of checkboxes, where they can click multiple Interests, and save them all in one hit. 我已经在用户必须选择多个兴趣的情况下使用它 - 他们只获得一个复选框列表,他们可以点击多个兴趣,并将它们全部保存在一个点击中。

In your case, it'll be easier to have a separate table with it's own model. 在您的情况下,使用它自己的模型单独使用表会更容易。 I'd call it Relationships or something similar. 我称之为关系或类似的东西。 It would have an id, followed_by_id, following_id, and any other fields you may need. 它将具有id,followed_by_id,following_id以及您可能需要的任何其他字段。

I've dug up some code from an old cake 1.3 app, but it should help you out. 我从一个旧的蛋糕1.3应用程序挖出一些代码,但它应该帮助你。 Your Relationships model would look something like this: 您的Relationships模型看起来像这样:

<?php
class Relationship extends AppModel {
    var $name = 'Relationship';

    var $belongsTo = array(
        'FollowedBy' => array(
            'className' => 'User',
            'foreignKey' => 'followed_by_id'
        ),
        'Following' => array(
            'className' => 'User',
            'foreignKey' => 'following_id'
        )
    );
}
?>

Your User's model would have to have relationships like this: 您的用户模型必须具有以下关系:

var $hasMany = array(
    'Followers' => array(
        'className' => 'Relationship',
        'foreignKey' => 'following_id',
        'dependent'=> true
    ),
    'FollowingUsers' => array(
        'className' => 'Relationship',
        'foreignKey' => 'followed_by_id',
        'dependent'=> true
    ),

);

Then in your relationships controller, you'd have methods something like this: 然后在你的关系控制器中,你会有类似这样的方法:

function add($following_id = null) {
    $this->Relationship->create();
    $this->Relationship->set('followed_by_id',$this->Auth->User('id'));
    $this->Relationship->set('following_id',$following_id);
    $this->Relationship->save();
    $this->redirect($this->referer());
}

function delete($id = null) {
    $this->Relationship->delete($id);
    $this->redirect($this->referer());
}

Note that in that code, I'm modifying the database with a GET request - which I really shouldn't be doing (it's old code, from years ago). 请注意,在该代码中,我正在使用GET请求修改数据库 - 我真的不应该这样做(这是几年前的旧代码)。 You'll want to enforce a POST request for both the add and delete methods, since they're modifying the database. 您将要对添加和删除方法强制执行POST请求,因为它们正在修改数据库。

But still, that code should set you on the right track. 但是,这段代码应该让你走上正轨。

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

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