OctoberCMS:如何保持双向友谊关系? [英] OctoberCMS: How to maintain a two-way friendship relation?
问题描述
我正在扩展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_id
和friend_id
添加到friends
表中,我也要自动将friend_id
和user_id
添加到表中.为此,我在FriendsPivot
模型中实现了afterSave
和beforeSave
:
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屋!