OctoberCMS:如何保持双向友谊关系? [英] OctoberCMS: How to maintain a two-way friendship relation?

查看:92
本文介绍了OctoberCMS:如何保持双向友谊关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展rainlab.user插件,以允许每个用户通过具有以下字段的简单中间表来结交朋友:

I'm extending the rainlab.user plugin to allow each user to have friends via a simple intermediate table with the following fields:

user_id
friend_id
status

我扩展了User模型:

use RainLab\User\Models\User as FrontUser;

FrontUser::extend(function($model) {

    $model->belongsToMany['friends']=[
        'RainLab\User\Models\User',
        'table'    => 'meysam_social_friends',
        'pivot' => ['status'],
        'pivotModel' => 'Meysam\Social\Models\FriendsPivot',
        'timestamps' => true,
        'key'      => 'user_id',
        'otherKey' => 'friend_id'
    ];

    $model->addDynamicMethod('isFriendWith', function (FrontUser $user) use ($model) {
        $model->friends->contains($user->id);
    });

    $model->addDynamicMethod('addFriend', function (FrontUser $user) use ($model) {
        $model->friends()->attach($user->id);
    });

    $model->addDynamicMethod('removeFriend', function (FrontUser $user) use ($model) {
        $model->friends()->detach($user->id);
    });
});

还扩展了Rainlab.User Controller以具有Friends选项卡,其中列出了用户的所有好友,并可将其添加和删除:

And also extended the Rainlab.User Controller to have Friends tab where all friends of a user are listed and can be added and removed:

use RainLab\User\Controllers\Users as UsersController;

UsersController::extend(function($controller) {

    if(!isset($controller->implement['Backend.Behaviors.RelationController'])) {
        $controller->implement[] = 'Backend.Behaviors.RelationController';
    }
    $controller->relationConfig  =  '$/meysam/social/controllers/user/config_relations.yaml';
});

UsersController::extendFormFields(function($form, $model, $context) {
    if(!$model instanceof FrontUser or $context != 'preview'){
        // friends tab should not be displayed in update and create contexts
        return;
    }

    $form->addTabFields([
        'friends' => [
            'label' => '',
            'tab' => 'Friends',
            'type' => 'partial',
            'path' => '$/meysam/social/controllers/user/_friends.htm',
        ]
    ]);
});

现在,我需要维持双向友谊关系.即无论何时将user_idfriend_id添加到friends表中,我也要自动将friend_iduser_id添加到表中.为此,我在FriendsPivot模型中实现了afterSavebeforeSave:

Now I need to maintain a two-way friendship relationship. i.e. whenever user_id and friend_id is added to the friends table, I want to automatically add friend_id and user_id to the table as well. To achieve this, I implemented afterSave and beforeSave in the FriendsPivot model:

class FriendsPivot extends Pivot
{
    /*
     * Validation
     */
    public $rules = [
        'status' => 'required'
    ];

    public $belongsTo = [
        'user' => ['RainLab\User\Models\User', 'key' => 'user_id'],
        'friend' => ['RainLab\User\Models\User', 'key' => 'friend_id']
    ];

    public function getStatusOptions()
    {
        return [
            1 => 'Pending',
            2 => 'Approved',
            3 => 'Blocked',
        ];
    }

    public function afterSave()
    {
        Log::info('Saving pivot...');

        if(!$this->friend->isFriendWith($this->user)) {
            $this->friend->addFriend($this->user);
        }
    }

    public function beforeDelete()
    {
        Log::info('Deleting pivot...');

        if($this->friend->isFriendWith($this->user)) {
            $this->friend->removeFriend($this->user);
        }
    }
}

问题是beforeDelete从未被调用. afterSave被调用,但beforeDelete从不被调用,因此,关系的逆关系不会被删除(user_id-friend_id从数据库中删除,但friend_id-user_id不会被删除).为什么不调用beforeDelete?我做错了什么吗?有没有更好的方法来维持双向友谊关系?

The problem is that beforeDelete is never called. afterSave gets called but beforeDelete never gets called and therefor the inverse of the relationship is not deleted (user_id-friend_id gets removed from database but friend_id-user_id does not get deleted). Why is beforeDelete not called? Is there anything I'm doing wrong? Is there any better way to maintain a two-way friendship relation?

推荐答案

我找到了这篇文章,是因为我试图做与您完全相同的事情.如果您已解决此问题,那么我想知道您是否愿意分享您的解决方案?

I found this post because I'm trying to do exactly the same thing as you. If you have solved this then I wonder if you would be willing to share your solution?

这篇关于OctoberCMS:如何保持双向友谊关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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